Android: Restrict activity orientation based on screen size

一世执手 提交于 2019-12-07 17:24:54

问题


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?


回答1:


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;
}


来源:https://stackoverflow.com/questions/10491531/android-restrict-activity-orientation-based-on-screen-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!