[英]How to set up orientation in manifest for tablets as landscape and portrait for phones?
[英]Portrait mode for Phones and Portrait+Landscape for Tablets
我正在構建一個通用的android應用,其中手機僅需要支持縱向,平板電腦將支持縱向和橫向。 這是我的布局結構
layout-port layout-sw600dp-port layout-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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.