Portrait mode for Phones and Portrait+Landscape for Tablets

I am building a universal android app, where phones need to support only portrait and tablets will support both portrait and landscape. Here is my layout structure

layout-port layout-sw600dp-port layout-sw600dp-land

But if the phone is in landscape mode, the app crashes, reasoning Resource not found Exception.


This way should work:Define boolean value inside values-sw600dp boolean.xml file.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="tablet">true</bool>
</resources>

Define similar file inside the regular values folder and chnage the value to false:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="tablet">false</bool>
</resources>

Put this in your onCreate:

boolean tablet = getResources().getBoolean(R.bool.tablet);
if (!tablet) 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

Hi you can set your orientation programmatically in the activity like this

if(isTable()){
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else{
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

where isTablet() method

public static boolean isTablet(Context context) {

    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;

}// isTablet
链接地址: http://www.djcxy.com/p/37958.html

上一篇: 设备/平板电脑的Android布局文件夹始终处于横向模式

下一篇: 肖像模式为电话和肖像+风景为片剂