I am developing an Android application and I'd like to support all device sizes, from phones to full-size tablets. I'd like to restrict the application to only using portrait orientation on smaller devices and allow a different, more large-screen friendly UI to be used in landscape when the device is larger. So far, I am unable to find any way, let alone a graceful way, to restrict an activity to allowing orientations based on the device's screen size.
An example would开发者_Go百科 be this: A smartphone running the app is only allowed to use the layout resources in layout-small and layout-normal, and is not allowed to use landscape at all. A larger, tablet device gets to use UIs that are more large-screen friendly, and is allowed to be used in both portrait and landscape, with unique UIs for each orientation.
Any tips would be greatly appreciated.
To force an Activity to go into portrait mode you can set it in the Android Manifest but this way you are not able specify it for smaller screens, I believe thats not possible. Also you should be careful doing this because certain phone are made for landscape mode. http://developer.android.com/guide/topics/manifest/activity-element.html#screen
To provide alternative layouts for specific screen sizes you can do it by adding parameters in the folder name
/res/layout-small/main.xml
/res/layout-normal/main.xml
/res/layout-large/main.xml
/res/layout-xlarge/main.xml
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
You could try getting the screen size programmatically using something like:
int width = display.getWidth();
int height = display.getHeight();
Then allow/unallow based on if the screensize is between specific boundries?
精彩评论