Android adaptation solution Summary (III)

Source: Internet
Author: User

Android adaptation solution Summary (III)
In the Android adaptation solution Summary (1) and (2), we have learned some basic concepts.
So what should we pay attention to during specific development.
First, we must know that the key to adaptation lies in two points:
(1)Adaptation of devices with different resolutionsIn this case, you can use dp, sp, and image resources to store in different drawable folders;
(2)Adaptation of different sizesThis is mainly solved by placing the relevant values and layout files in different folders.
2.1 values folder
You can create different values folders under projects: values-sw480dp, values-sw600dp,
Values-sw720dp-land and so on. For example, the width of a control is 10dp on the 10-inch pad and 8-inch pad.
Is 5dp. At this point, you can define a variable, button_width, and then in the values-sw600dp
Write-down 5dp , Write in values-sw720-land
10dp . In this way, it is achieved on pad of different sizes,
The corresponding controls have different sizes.
2.1 layout folder
If the layout displayed on devices of different sizes is significantly different, it is difficult to control the layout only by using values,
Therefore, you can consider placing different layout files in different layout folders.
Size to load the layout file in the corresponding folder. Such as: layout-sw480dp, layout-sw600dp,
Layout-sw700dp and so on.
It is worth noting that, if not necessary, we should try to adopt the 2.1 solution to facilitate maintenance. If the size and resolution are different,
Therefore, we need to consider (1) and (2.

(Supplement: in fact, the values folder and the layout folder are not only determined based on the size, but also related to the resolution, but usually,

Considering the overall calculation, you can only determine the size:

320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).480dp: a tweener tablet like the Streak (480x800 mdpi).600dp: a 7” tablet (600x1024 mdpi).720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).
600dp indicates the shortest side of the device.

The code for obtaining the shortest edge of a device is: Configuration config = getResources (). getConfiguration ();

Int smallestScreenWidth = config. smallestScreenWidthDp;

In this case, compare smallestScreenWidth with 600 to see if the device can read the resources in the device.

)


In addition, for convenience of adaptation, what should we pay attention to during encoding? The main points are as follows:

(1) Multiple Weights (android: layout_weight)

Especially in the case of tab switching layout, listview title and Item layout;
(2) When setting the width and height, use match_parent and wrap_content whenever possible to avoid setting the width and height of the control to death;
(3) parent container layout Selection
Use RelativeLayout, FrameLayout, and GridLayout to reduce the layout level. Of course
You must use LinearLayout for weight calculation;
(4) In xml, set the height and width to dp (dip), and set the font to sp.

(Note that in the Code, the unit of setHeight (...) We write is px)



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.