简体   繁体   中英

Setting the device orientation based on the device type

I know that in iOS I am able to set the app orientation within the .plist file so that a phone can only be in portrait while the pad can be in portrait or landscape

Is there something similar under Android or is it (as I suspect) that devices are all treated the same so can't be locked?

This subject is discussed in the Android Training:

http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali

If you read the entire topic, they explain how to set a boolean value in a specific value file (as res/values-sw600dp/):

<resources>
    <bool name="isTablet">true</bool>
</resources>

Because the sw600dp qualifier is only valid for platforms above android 3.2. If you want to make sure this technique works on all platforms (before 3.2), create the same file in res/values-xlarge folder:

<resources>
    <bool name="isTablet">true</bool>
</resources>

Then, in the "standard" value file (as res/values/), you set the boolean to false:

<resources>
    <bool name="isTablet">false</bool>
</resources>

Then in you activity, you can get this value and check if you are running in a tablet size device:

boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
    // do something
} else {
    // do something else
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM