A: Different layout
Android phone screen size is different, there are 480x320, 640x360, 800x480. How can I get the app to adapt to various screens automatically?
In fact, it is very simple to create a different layout folder in the Res directory, such as layout-640x360,layout-800x480, All layout files are written to R.java after compilation, and the system uses the appropriate layout according to the size of the screen.
II: HDPI, MDPI, ldpi
In the previous version, there was only one drawable, while the 2.1 version had drawable-mdpi, drawable-ldpi, drawable-hdpi Three, and the three were mostly to support multi-resolution.
The difference between drawable-hdpi, drawable-mdpi and drawable-ldpi:
(1) drawable-hdpi contains high-resolution images, such as WVGA (480x800), FWVGA (480x854)
(2) drawable-mdpi contains medium-resolution images, such as HVGA (320x480)
(3) drawable-ldpi contains low-resolution images, such as QVGA (240x320)
The system will be based on the resolution of the machine to each of these folders to find the corresponding picture.
In developing the program in order to be compatible with different screens on different platforms, it is recommended that the respective folders store different versions of the images according to requirements.
Screen Orientation:
Horizontal screen vertical screen automatically switch:
The Res directory can be set up in the Layout-port and Layout-land two directories, which are placed in vertical screen and horizontal screen two layout files, so that the mobile phone screen direction changes when the system will automatically call the corresponding layout files, to avoid a layout file can not meet the two screen display problems.
Do not switch:
The following steps are circulated online, but I myself was through a graphical interface to achieve this configuration, is the same as the same, I will be free to paste the picture.
Also note that each activity has this attribute screenorientation, each activity needs to be set, can be set to vertical screen (portrait), can also be set to no gravity sense (nosensor).
To keep the program interface in one Direction, do not change depending on the direction of the phone:
It can be configured in Androidmanifest.xml. Join this line android:screenorientation= "Landscape".
For example (landscape is landscape, Portrait is portrait):
Java code:
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.ray.linkit"Android:versioncode= "1"Android:versionname= "1.0"> <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name"> <ActivityAndroid:name=". Main "Android:label= "@string/app_name"android:screenorientation= "Portrait"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <ActivityAndroid:name=". GamePlay "android:screenorientation= "Portrait"></Activity> <ActivityAndroid:name=". Optionview "android:screenorientation= "Portrait"></Activity> </Application> <USES-SDKandroid:minsdkversion= "3" /></Manifest>
In addition, the switch of each screen in Android will restart activity, so you should save the current active state before the activity is destroyed, and load the configuration when the activity is re-create, so the game in progress will not be restarted automatically!
Some programs are suitable to switch from vertical screen to horizontal screen, or vice versa, how to do this time? The following configuration android:screenorientation= "Portrait" can be configured where the activity is configured. This will ensure that the vertical screen is always vertical, or landscape landscape.
And some programs are suitable for the screen switch. How to deal with it? The first thing to do when configuring activity is to configure the following: Android:configchanges= "Keyboardhidden|orientation", You also need to rewrite the activity's Onconfigurationchanged method. The implementation is as follows, do not need to do too much content:
@Override Public voidonconfigurationchanged (Configuration newconfig) {Super. onconfigurationchanged (Newconfig); if( This. Getresources (). GetConfiguration (). Orientation = =Configuration.orientation_landscape) { //Land does nothing is OK}Else if( This. Getresources (). GetConfiguration (). Orientation = =configuration.orientation_portrait) { //Port do nothing is OK } }
Android Adaptive screen orientation and size