Android Adaptation Program summary (iii)

Source: Internet
Author: User

In the Android Adaptation program summary (i), (ii), we have learned some basic concepts.
So what should we pay attention to in the concrete development?
First of all, we have to know that the key to adaptation is two points:
(1)adaptation of devices with different resolutions, this can be solved by using DP, SP and picture resources in different drawable folders for the use of the Unit;
(2)suitable for different sizes, which is mainly resolved by placing the relevant values and layout files in different folders.
2.1 Values Folder
You can create different values folders under the project: VALUES-SW480DP, VALUES-SW600DP,
Values-sw720dp-land and so on. such as the width of a control, on the 10-inch pad is 10DP, in 8-inch pad
On the 5DP. At this point, you can define a variable, button_width, and then VALUES-SW600DP
Write down <dimen name= "Button_width" &GT;5DP&LT;/DIMEN&GT;, write under Values-sw720-land
<dimen name= "Button_width" >10dp</dimen>. This is achieved in the different size of the pad,
The corresponding control has a different size effect.
2.1 Layout Folder
If there are obvious differences in the layout shown on different sizes of devices, it is difficult to control just by using values.
Then you can consider writing different layout files in different layouts folder, Android will be based on the device
Size to load the layout file under the appropriate folder. such as: LAYOUT-SW480DP,LAYOUT-SW600DP,
LAYOUT-SW700DP and so on.
It is worth noting that if it is not necessary, as far as possible to adopt the 2.1 scheme, easy to maintain. If the size and resolution are different,
Then it is necessary to combine (1), (2) to consider.

(add: In fact, the values folder and Layout folder is not only judged by size, but also with resolution, but under normal circumstances,

Considering the overall calculation, it is possible to judge by size only:

320DP:A Typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc.). 480dp:a Tweener tablet like the Streak (480 X800 mdpi). 600dp:a 7 "tablet (600x1024 mdpi). 720dp:a" Tablet (720x1280 mdpi, 800x1280 mdpi, etc).
The meaning of 600DP is: the shortest side that represents this device.

The code to get the shortest side of the device is:Configuration config = getresources (). GetConfiguration ();

int smallestscreenwidth = CONFIG.SMALLESTSCREENWIDTHDP;

This time take smallestscreenwidth and 600 to compare can know whether the device can read the resources inside.

)


In addition, in order to facilitate the adaptation, in the code we should also pay attention to what, the main points are as follows:

(1) Multi-use weight (android:layout_weight)

Especially in the case of tab switch layout, ListView title and item layout;
(2) When setting width and height, use match_parent and wrap_content as far as possible to avoid the height of the control to die;
(3) parent container layout selection
Use Relativelayout,framelayout,gridlayout and more to reduce layout levels. Of course, when using
Weight, you have to use linearlayout;
(4) in XML, set the height, width with DP (DIP), set the font using SP.

(It should be noted that in the code we wrote the SetHeight (...) Unit is PX)



Android Adaptation Program summary (iii)

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.