肖像模式为电话和肖像+风景为片剂
我正在构建一个通用的Android应用程序,手机只需支持肖像和平板电脑即可支持肖像和风景。 这是我的布局结构
布局 - 端口布局 - sw600dp-port布局 - sw600dp-land
但是,如果手机处于横向模式,应用程序崩溃,推理资源未找到异常。
这种方式应该工作:定义values-sw600dp boolean.xml文件中的布尔值。
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="tablet">true</bool>
</resources>
在常规值文件夹内定义类似的文件,并将该值设置为false:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="tablet">false</bool>
</resources>
把它放在你的onCreate中:
boolean tablet = getResources().getBoolean(R.bool.tablet);
if (!tablet) 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
嗨,你可以在这样的活动中以编程方式设置你的方向
if(isTable()){
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else{
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
  其中isTablet()方法 
public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}// isTablet
上一篇: Portrait mode for Phones and Portrait+Landscape for Tablets
下一篇: How to lock android app's orientation to portrait mode?
