安卓:根据屏幕大小限制的活动方向 [英] Android: Restrict activity orientation based on screen size

查看:159
本文介绍了安卓:根据屏幕大小限制的活动方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它看起来在两个方向上的一个或大或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;
}

这篇关于安卓:根据屏幕大小限制的活动方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆