I. Different layout
AndroidThe screen sizes of mobile phones vary by 480x320,640x360,800 x. How can an App automatically adapt to different screens?
In fact, it is very simple, just need to create different layout folder under the res directory, such as layout-640x360, layout-800x480, all the layout files will be written to R after compilation. java, and the system selects the appropriate layout based on the screen size.
Ii. hdpi, mdpi, and ldpi
In earlier versions, there was only one drawable, while in version 2.1, there were three types: drawable-mdpi, drawable-ldpi, and drawable-hdpi, which were mainly used to support multi-resolution.
Differences between drawable-hdpi, drawable-mdpi, and drawable-ldpi:
(1) The drawable-hdpi contains high-resolution images, such as WVGA (480x800) and FWVGA (480x854)
(2) drawable-mdpi stores medium-resolution images, such as HVGA (320x480)
(3) drawable-ldpi stores low-resolution images, such as QVGA (240x320)
The system will find the corresponding images in these folders according to the machine resolution.
3. Landscape Screen
To be compatible with different screens on different platforms, we recommend that you store images of different versions in different folders as needed. The following steps are circulated on the Internet, but I used to implement this configuration through a graphical interface, which is the same in different ways. I will paste the image when I have time.
Note: Each activity has this attribute screenOrientation, which must be set for each activity. You can set this attribute to portrait screen portrait or gravity-free nosensor ).
Solution to keep the program interface in one direction and not change with the orientation of the mobile phone:
Configure AndroidManifest. xml. Add this line
- android:screenOrientation="landscape"
For example, if landscape is horizontal and portrait is vertical ):
Java code:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.ray.linkit"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".Main"
- android:label="@string/app_name"
- android:screenOrientation="portrait">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".GamePlay"
- android:screenOrientation="portrait"></activity>
- <activity android:name=".OptionView"
- android:screenOrientation="portrait"></activity>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- </manifest>
In addition, the Activity is restarted every time the screen is switched on in android. Therefore, you should save the status of the current Activity before the Activity is destroyed and load the configuration when the Activity is created again, in progress, the game will not automatically restart!
Some programs are suitable for switching from a portrait screen to a landscape screen, or vice versa. What should we do at this time? You can configure android: screenOrientation = "portrait" in the Activity configuration area ". In this way, you can ensure that the portrait screen is always portrait or landscape.
Some programs are suitable for switching between landscape and landscape screens. What should we do? First, you must configure the Activity as follows: android: configChanges = "keyboardHidden | orientation". In addition, you must override the onConfigurationChanged method of the Activity. The implementation method is as follows:
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
- // land do nothing is ok
- } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
- // port do nothing is ok
- }
- }
Android Study Notes: Activity jump
Insert a new Activity in Android Development
Android development: Passing values between activities
Android fun five is not "authentic" Linux