How to Implement UI self-adaptation in the Android system

Source: Internet
Author: User

Http://www.cnblogs.com/melaniedeng/archive/2012/05/17/2506869.html

   
   
   

Everyone who works on Android applications knows that it is easy to apply an APK to multiple different mobile phone screens, that is, there are multiple sets of related resource files under the res folder of the project. When the program is running, the android system loads resource files in different folders based on the information of the current device. But how does the Android system achieve this? I searched on the Internet and found few convenient introductions. I had to study the code myself. The following is the result of my research Code (the correctness is to be confirmed). Here we will share it.

Here, we use the oncreate () method of activity on ICS to call setcontentview (INT resourceid) as an example to describe how the system uses our ID (R. layout. XXXX) Find the appropriate layout file for parsing and loading:

If your res has three different layout: layout, layout-sw480dp, and layout-sw600dp, the SW <n> DP indicates that the layout file under the layout folder is loaded only when the minimum bandwidth of the device's short side is N. Your device is X in resolution, then this APK installed on your device will load the layout file in the layout-sw480dp. The following is the Java-layer call chain of the framework:

Activity. setcontentview (INT resourceid)-> phonewindow. setcontentview (INT resourceid)-> layoutinflater. inflate (INT resource, viewgroup root)-> layoutinflater. inflate (INT resource, viewgroup root, Boolean attachtoroot)-> resources. getlayout (int id)->
Resources. loadxmlresourceparser (int id, string type)-> resources. getvalue (int id, typedvalue outvalue, Boolean resolverefs)-> assetmanager. getresourcevalue (INT Ident, int density, typedvalue outvalue, Boolean resolverefs)-> assetmanager. loadresourcevalue (int
Ident, short density, typedvalue outvalue, Boolean resolve)

In the above drop chain:

1. which XML is finally loaded by resources. getvalue (int id, typedvalue outvalue, Boolean resolverefs) indicates the outvalue after the call is completed. string, because outvalue. the string value is the specific path of your resource file, such:

1) xxx/values/xxx. xml

2) xxx/layout-sw600dp/xxx. xml

2. assetmanager. loadresourcevalue () is the native method in frameworks/base/CORE/JNI/android_util_assetmanager.cpp. To obtain the correct outvalue, the native method consists of the following steps:

1) Call the restable: getresource () of frameworks/base/libs/utils/resourcetypes. cpp to traverse all resource files.

2) In restable: getresource (), call restable: getentry () to determine the entry from which the resource file comes, that is, layout or layout-SW <n> DP, restable: getentry () is the key to our problem.

3) In restable: getentry:

A) First, obtain the information about the current device, screen resolution, screen size, locale, and landscape.

B) filter out the entry that does not adapt to the device based on the received information of the device, such as the device is 800x133, then more resources than this resolution (e.g.: layout-sw600dp) to be filtered out and implemented in frameworks/base/include/utils/resourcetypes. in the match function of restable_config in H

C) make the best adaptation to the filtered resource and find the most suitable entry file. Because we have already filtered out the non-conformities, that is, the entry with a large resolution, the best adaptation is found here. Implemented in the isbetterthan () function of restable_config in frameworks/base/include/utils/resourcetypes. h.

3. I made an attempt to load the resource files in the layout-sw600dp to applications on devices with X resolution. So change the match function of restable_config in frameworks/base/include/utils/resourcetypes. h In Step B as follows:

?
/*if (smallestScreenWidthDp != 0&& smallestScreenWidthDp > settings.smallestScreenWidthDp){return false;}*/if
(smallestScreenWidthDp != 0
&& smallestScreenWidthDp > 600) {return
false;}

I will set settings. smallestscreenwidthdp is forcibly replaced with 600. In this way, all resource files smaller than 600dp (including 600) are retained during filtering, and C is not checked, only find the largest, so layout-sw600dp has become the system that "the most suitable" resource price.

Push the Lib library generated by recompiling frameworks/base/libs/utils/to/system/libs, restart the mobile phone, and then start the above application, you can see the UI of the layout-sw600dp loaded by the program.

    

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.