安卓:根据屏幕大小限制的活动方向
问题描述:
我有一个应用程序,它看起来在两个方向上的一个或大或XLARGE屏幕尺寸好,但应仅限于肖像在手机上。我知道,你可以限制清单中的活动的方向,但有没有办法这样做的条件?或者是有一些种类的财产,我可以在活动本身来选择支持哪些方向设置?
I have an app that looks good in either orientation on a large or xlarge screen size, but should be restricted to portrait on phones. I know that you can restrict the orientation of an Activity in the manifest, but is there a way to do so conditionally? Or is there some kind of property I can set in the activity itself to choose which orientations are supported?
答
这是我在我的应用程序使用该解决方案:
This is the solution I use in my apps:
public void onCreate(Bundle savedState) {
//...
if(isScreenLarge()) {
// width > height, better to use Landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
public boolean isScreenLarge() {
final int screenSize = getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK;
return screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE
|| screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
}