Android automatically adapts to screen 2. Custom Size Method

Source: Internet
Author: User

This is a self-developed method. It may be a relatively stupid method. Therefore, not many people have proposed this method to solve the adaptive problem. Although this method has many disadvantages, it is sometimes a good method.
First look at the figure below



We can see that I have defined two sets of size files. We can see one of them.

? Code snippet, double-click to copy 01020304050607080910111213141516171819202122232425262728293031323334353637383940414243 <? Xml
Version = "1.0"

Encoding = "UTF-8"?> <Resources> <dimen
Name = "height_000080"> 6px </dimen> <dimen
Name = "height_2_80"> 12px </dimen> <dimen
Name = "height_3_80"> 18px </dimen> <dimen
Name = "height_4_80"> 24px </dimen> <dimen
Name = "height_5_80"> 30px </dimen> <dimen
Name = "height_6_80"> 36px </dimen> <dimen
Name = "height_7_80"> 42px </dimen> <dimen
Name = "height_8_80"> 48px </dimen> <dimen
Name = "height_9_80"> 54px </dimen> <dimen
Name = "height_10_80"> 60px </dimen> <dimen
Name = "height_11_80"> 66px </dimen> <dimen
Name = "height_12_80"> 72px </dimen> <dimen
Name = "height_13_80"> 78px </dimen> <dimen
Name = "height_14_80"> 84px </dimen> <dimen
Name = "height_15_80"> 90px </dimen> <dimen
Name = "height_16_80"> 96px </dimen> <dimen
Name = "height_17_80"> 102px </dimen> <dimen
Name = "height_18_80"> pixel PX </dimen> <dimen
Name = "height_19_80"> 114px </dimen> <dimen
Name = "height_20_80"> 120px </dimen> <dimen
Name = "height_21_80"> 126px </dimen> <dimen
Name = "height_22_80"> 132px </dimen> <dimen
Name = "height_23_80"> 138px </dimen> <dimen
Name = "height_24_80"> 144px </dimen> <dimen
Name = "height_25_80"> 150px </dimen> <dimen
Name = "height_26_80"> 156px </dimen> <dimen
Name = "height_27_80"> 162px </dimen> <dimen
Name = "height_28_80"> 168px </dimen> <dimen
Name = "height_29_80"> 174px </dimen> <dimen
Name = "height_30_80"> 180px </dimen> <dimen
Name = "height_31_80"> 186px </dimen> <dimen
Name = "height_32_80"> 192px </dimen> <dimen
Name = "height_33_80"> 198px </dimen> <dimen
Name = "height_34_80"> 204px </dimen> <dimen
Name = "height_35_80"> 210px </dimen> <dimen
Name = "height_36_80"> 216px </dimen> <dimen
Name = "height_37_80"> 222px </dimen> <dimen
Name = "height_38_80"> 228px </dimen> <dimen
Name = "height_39_80"> 234px </dimen> <dimen
Name = "height_40_80"> 240px </dimen> <dimen
Name = "height_41_80"> 246px </dimen> <dimen
Name = "height_42_80"> 252px </dimen> <dimen
Name = "height_43_80"> 258px </dimen> <dimen
Name = "height_44_80"> pixel PX </dimen> <dimen
Name = "height_45_80"> 270px </dimen> <dimen
Name = "height_46_80"> 276px </dimen> <dimen
Name = "height_47_80"> 282px </dimen> <dimen
Name = "height_48_80"> 288px </dimen> <dimen
Name = "height_49_80"> 294px </dimen> <dimen
Name = "height_50_80"> 300px </dimen> <dimen
Name = "height_51_80"> 306px </dimen> <dimen
Name = "height_52_80"> 312px </dimen> <dimen
Name = "height_53_80"> 318px </dimen> <dimen
Name = "height_54_80"> 324px </dimen> <dimen
Name = "height_55_80"> 330px </dimen> <dimen
Name = "height_56_80"> 336px </dimen> <dimen
Name = "height_57_80"> 342px </dimen> <dimen
Name = "height_58_80"> 348px </dimen> <dimen
Name = "height_59_80"> 354px </dimen> <dimen
Name = "height_60_80"> 360px </dimen> <dimen
Name = "height_61_80"> pixel PX </dimen> <dimen
Name = "height_62_80"> 372px </dimen> <dimen
Name = "height_63_80"> pixel PX </dimen> <dimen
Name = "height_64_80"> 384px </dimen> <dimen
Name = "height_65_80"> 390px </dimen> <dimen
Name = "height_66_80"> pixel PX </dimen> <dimen
Name = "height_67_80"> 402px </dimen> <dimen
Name = "height_68_80"> 408px </dimen> <dimen
Name = "height_69_80"> 414px </dimen> <dimen
Name = "height_70_80"> 420px </dimen> <dimen
Name = "height_71_80"> pixel PX </dimen> <dimen
Name = "height_72_80"> 432px </dimen> <dimen
Name = "height_73_80"> 438px </dimen> <dimen
Name = "height_74_80"> 444px </dimen> <dimen
Name = "height_75_80"> pixel PX </dimen> <dimen
Name = "height_76_80"> 456px </dimen> <dimen
Name = "height_77_80"> 462px </dimen> <dimen
Name = "height_78_80"> 468px </dimen> <dimen
Name = "height_79_80"> 474px </dimen> <dimen
Name = "height_80_80"> 480px </dimen>
</Resources>

 

This is the code in the dimens_height.xml file under the values-480x320 folder, I divide the entire height into 80 equal points, this is because most of the screen width or height is an integer multiple of 80 (except for some special ), different equi sets different size values in different resolutions.
Since each set of interfaces should be written into one set, some may feel bad, but it is relatively simple to write and does not need to be changed in the future, so sometimes you can also consider using it!
Let's look at the layout code of my Demo (dimen_layout.xml)


? Code snippet, double-click to copy 010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748 <? Xml
Version = "1.0"

Encoding = "UTF-8"?> <LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<View
Android: layout_width = "@ dimen/width_76_80"
Android: layout_height = "@ dimen/height_10_80"
Android: background = "# ffcccc"
Android: layout_margin = "@ dimen/width_2_80"/>

<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<View
Android: layout_width = "@ dimen/width_30_80"
Android: layout_height = "@ dimen/height_50_80"
Android: background = "# ccccff"
Android: layout_margin = "@ dimen/height_5_80"/>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">

<Button
Android: layout_width = "@ dimen/width_30_80"
Android: layout_height = "@ dimen/height_5_80"
Android: background = "# ccffcc"
Android: layout_marginBottom = "@ dimen/height_000080"
Android: text = "5"/>
<Button
Android: layout_width = "@ dimen/width_30_80"
Android: layout_height = "@ dimen/height_10_80"
Android: background = "# ccffcc"
Android: layout_marginBottom = "@ dimen/height_000080"
Android: text = "10"/>
<Button
Android: layout_width = "@ dimen/width_30_80"
Android: layout_height = "@ dimen/height_15_80"
Android: background = "# ccffcc"
Android: layout_marginBottom = "@ dimen/height_000080"
Android: text = "15"/>
<Button
Android: layout_width = "@ dimen/width_30_80"
Android: layout_height = "@ dimen/height_20_80"
Android: background = "# ccffcc"
Android: text = "20"/>
</LinearLayout>

</LinearLayout>
</LinearLayout>


The above is the unified layout code I wrote, let's take a look at the display effect on two simulators with different resolutions (note that custom dimensions are also used for values like margin in my code. If this margin is controlled using layout_weight, there is no doubt that more linear la s need to be nested, so none of them is perfect. These 2nd methods are sometimes easier to use)
 

 

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.