Horizontal screen and vertical screen with different resolutions

Source: Internet
Author: User

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

 
 
  1. android:screenOrientation="landscape" 

For example, if landscape is horizontal and portrait is vertical ):

Java code:

 
 
  1. <?xml version="1.0" encoding="utf-8"?>      
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"      
  3.       package="com.ray.linkit"      
  4.       android:versionCode="1"      
  5.       android:versionName="1.0">      
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">      
  7.         <activity android:name=".Main"      
  8.                   android:label="@string/app_name"      
  9.                   android:screenOrientation="portrait">      
  10.             <intent-filter>      
  11.                 <action android:name="android.intent.action.MAIN" />      
  12.                 <category android:name="android.intent.category.LAUNCHER" />      
  13.             </intent-filter>      
  14.         </activity>      
  15.                 <activity android:name=".GamePlay"      
  16.                 android:screenOrientation="portrait"></activity>      
  17.                 <activity android:name=".OptionView"      
  18.                 android:screenOrientation="portrait"></activity>      
  19.     </application>      
  20.     <uses-sdk android:minSdkVersion="3" />      
  21. </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:

 
 
  1. @Override  
  2.         public void onConfigurationChanged(Configuration newConfig) {  
  3.                 super.onConfigurationChanged(newConfig);  
  4.                 if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {  
  5.                         // land do nothing is ok  
  6.                 } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {  
  7.                         // port do nothing is ok  
  8.                 }  
  9.         }   

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.