[英]How to set up orientation in manifest for tablets as landscape and portrait for phones?
How to set up orientation in manifest for tablets as landscape and portrait for phones? 如何将平板电脑清单中的方向设置为手机的横向和纵向?
I want: 我想要:
<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"
....
In one manifest. 在一个清单中。 Is it possible?
可能吗?
No it's not possible , you have to check that on runtime . 不,这是不可能的,您必须在运行时检查一下。
Check if it's Tablet by : 通过以下方式检查它是否为平板电脑:
public static boolean isTablet(Context context)
{
return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
and then in your Activity, set orientation like this before setContentView() 然后在您的活动中,在setContentView()之前设置像这样的方向
if(isTablet(this))
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
You can check at run time by calling below method: 您可以在运行时通过调用以下方法进行检查:
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;
}
and you can set by orientation by calling in onCreate method 您可以通过调用onCreate方法按方向进行设置
if(isTablet(this)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.