Android screen self-adaption Multi-Resolution

Source: Internet
Author: User

From: http://blog.csdn.net/soesa/article/details/8047203

Although it is not a technical problem to adapt an application to different mobile phones, it is not a simple task for developers who are just working on the screen.


First, you need to add sub-elements in the <manifest> element of the androidmanifest. xml file as follows:
<Supports-screens Android: largescreens = "true"
Android: normalscreens = "true" Android: anydensity = "true"
Android: smallscreens = "true"> </supports-screens>

As expected, the above is to set multi-resolution support for our screens (more accurately, it is to adapt to three types of density: large, medium, and small ). Android: anydensity = "true", which plays a very important role in the entire screen. The value is true. When our applications are installed on mobile phones of different density, the program loads Resources in the hdpi, mdpi, and ldpi folders respectively.
On the contrary, if the value is set to false, even if we have the same resource in the hdpi, mdpi, and ldpi folders, the application will not automatically search for resources in the corresponding folder, this situation occurs on high-density and low-density mobile phones. For example, if Android: anydensity = "false" is set for a 240x320 pixel mobile phone ", the Android system will convert 240x320 (low density) to 320x480 (medium density). In this way, the application will load Resources in the mdpi file on the cell phone with low density.

2. careful people will find that the drawable file has been replaced by three folders, drawable-hdpi, drawable-mdpi, and drawable-ldpi, since the beginning of android2.0, some programmers intentionally re-create the drawable folder in the app after android to allow the app to load some images by default. In fact, this is completely unnecessary, according to the analysis in the first section, Android: anydensity = "false" is used to convert the size and density of an application to the medium density to load Resources in the mdpi. Similarly, when Android: anydensity = "false", the application loads Resources in the mdpi.
Summary:
First: Android: anydensity = "true", the system will automatically find the corresponding folder Based on the screen Density
Second: Android: anydensity = "false ",
(1) If the drawable-hdpi, drawable-mdpi, and drawable-ldpi folders have different density representations of the same image resource, the system will load the resources in the drawable_mdpi folder.
(2) If drawable-HPDI contains high-density images and there is no corresponding image resource in the other two folders, the system will load resources in drawable-hdpi.
(3) If drawable-hdpi, drawable-mdpi has image resources, and drawable-ldpi does not have corresponding image resources, the system will load Resources in the drawable-mdpi folder.

3. Note the different representation of various folders.
Drawable-hdpi this image is suitable for horizontal screen and vertical screen
Drawable-land-hdpi: loads resources in this folder when the screen is landscape and high-density
Drawable-Port-hdpi: loads resources in this folder when the screen is portrait and high-density

3. Sometimes, you can dynamically set a value in the Code as needed. For example, the relative offset of the map pin and map address prompt box is different on mobile phones of different density. At this time, you can use the following method to find the screen density:

Displaymetrics metric = new displaymetrics ();
Getwindowmanager (). getdefaultdisplay (). getmetrics (metric );
Int densitydpi = metric. densitydpi; // screen density DPI (120/160/240)

Then, you can set the cheap quantity for the density in the code.

However, it is best not to use this method. The best way is to set the phone numbers of different density in the XML file separately.
Here, the map offset can be set in the dimens. xml file in the values-HPDI, values-mdpi, and values-ldpi folders.
It is worth mentioning that:
<Dimen name = "bitmap_common_topoffset"> 40dp </dimen>
<Dimen name = "bitmap_common_bottomoffset">-14dp </dimen>
The negative number is totally effective, and the system will think it is a negative value.


4. Major mobile phone manufacturers make more or less changes to the Android operating system. Of course, these changes will have some impact on our applications.
For example:
(1) package name of the aidl file connecting to the music service in the system source code: COM. Android. Music
(2) LG may modify the package where the aidl file is located (for example, to com. android. music. player), and modify the file content (add a method, reduce a few methods, or modify the method name). Then, if our application wants to publish on LG's mobile phone, then we must change the aidl file to be connected, which must be exactly the same as that modified by the LG manufacturer.
2. solution 2:
2. layout adaptation:
At, we have solved the problem of resource image adaptation, but different image sizes will inevitably lead to different la S. If you use a unified layout file, resource images may not be fully displayed on the screen. Therefore, the solution is to create different layout folders and write specific images for different screen resolutions. XML file.
Multiple layout folders have corresponding naming rules, take resolution 480x854 as an example, need to create a 480x854 pixel layout folder, name: layout-854x480, there are two points need to note: ① large number (854) it must be in front of the letter; otherwise, an error is reported. ② the symbol between two digits is a lowercase English letter "X", not a multiplication number. The system will go to these folders to find the corresponding layout files based on the machine resolution.

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.