[英]How to Lock Android App's Orientation to Portrait in Phones and Landscape in Tablets?
[英]How to set up orientation in manifest for tablets as landscape and portrait for phones?
如何將平板電腦清單中的方向設置為手機的橫向和縱向?
我想要:
<application
android:name=".App"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:installLocation="preferExternal"
android:label="@string/app_name"
android:largeHeap="true"
<!--for phones-->
android:screenOrientation="portrait"
<!--for tablets-->
android:screenOrientation="landscape"
....
在一個清單中。 可能嗎?
不,這是不可能的,您必須在運行時檢查一下。
通過以下方式檢查它是否為平板電腦:
public static boolean isTablet(Context context)
{
return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
然后在您的活動中,在setContentView()之前設置像這樣的方向
if(isTablet(this))
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
您可以在運行時通過調用以下方法進行檢查:
boolean isTablet(Context context) {
boolean isTablet = false;
DisplayMetrics metrics = context.getApplicationContext().getResources()
.getDisplayMetrics();
Display display = ((WindowManager) context.getSystemService("window"))
.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
float density = metrics.density;
if ((width / density >= 600.0F) && (height / density >= 600.0F))
isTablet = true;
else {
isTablet = false;
}
return isTablet;
}
您可以通過調用onCreate方法按方向進行設置
if(isTablet(this)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.