Portrait for phone, landscape for Tablet (Android-Layout)

后端 未结 5 1677
臣服心动
臣服心动 2020-11-30 04:09

So I\'m making an application for Android and I want to force Landscape orientation for tablets and Portrait orientation for phones. However, it seems as though I can only

相关标签:
5条回答
  • 2020-11-30 04:34

    for people who used

    android:screenOrientation="nosensor"
    

    and did not work on it own I used it in addition to this snippet in my base activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                && (this.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) < Configuration.SCREENLAYOUT_SIZE_LARGE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }else if((this.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        super.onCreate(savedInstanceState);
    }
    
    0 讨论(0)
  • 2020-11-30 04:36

    Setting a particular orientation based on device density may not work because there are phones which have higher densities than tablets.

    What I did was to disable the device's orientation sensor by setting the attribute in the activity tag in the manifest file like this:

    android:screenOrientation="nosensor"
    

    When you run your app, by default portrait orientation is set for phones and landscape for tablets(and hence it'll select the xml file from layout-xlarge-land). And since you've set an orientation lock, it remains in this orientation.

    0 讨论(0)
  • 2020-11-30 04:46

    set a layout called llTablet on the layout menu in the "layout-large" folder layouts and a llPhone layout on the layout menu in the "layout" folder. By menu am refering to the first layout the user is entering. Then check if you can reference it. If you can't reference llPhone then its a tablet.

    llPhone = (LinearLayout) findViewById (R.id.llPhone)
    if (llPhone == null) {
     tablet = true;
    }
    else {
     tablet = false;
    }
    
    0 讨论(0)
  • 2020-11-30 04:51

    You can measure the actual size (in inches) of the device and then programmatically set the orientation using:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

    or

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    

    To get the physical size of the device you can use the code published here.

    It is not 100% accurate, but it is good enough to decide what king of device is running the app.

    0 讨论(0)
  • 2020-11-30 04:55

    I'm not entirely sure what you're saying, but it would seem that you should just measure the resolution and act accordingly. I mean, how do you know if it's a phone or a tablet OTHER than the resolution being different?

    0 讨论(0)
提交回复
热议问题