4 Kinds of Android screen adaptive Solutions _android

Source: Internet
Author: User
Tags constant

Android supports a multiple-screen mechanism that provides an appropriate way to manage and resolve application resources for the current device screen. This article describes the Android screen adaptive solution in 4.

First, to elaborate layout_weight

The most recommended Android multi-screen adaptive solution at the moment.

The function of this property is to determine the weight of the control's display in its parent layout, typically in a linear layout. The smaller the value, the higher the priority of the corresponding layout_width or layout_height, in general landscape layout, the decision is layout_width priority; In the vertical layout, the Layout_height priority is determined.

The traditional method of Layout_weight is to set the Layout_width and layout_height of the current control to fill_parent so that the display scale of the control can be completely given to layout_weight; The smaller the layout_weight, the larger the display ratio. However, for 2 controls are fine, if the control too much, and the display ratio is not the same time, control is more troublesome, after all, the inverse ratio is not so good to determine.

So there is now the most popular 0px set value method. Seemingly difficult to understand the layout_height=0px of the writing, combined with layout_weight, but can make the control into a proportional example, easily solve the current Android development headaches one of the most vexing problems.

First look at the styles (style_layout.xml)

<?xml version= "1.0" encoding= "Utf-8"?>
<resources> 
<!--full screen stretching--> <style name=
 " Layout_full "> 
  <item name=" android:layout_width ">fill_parent</item> 
  <item name=" Android : Layout_height ">fill_parent</item> 
 </style> 
<!--fixing its own size-->
 <style name=" Layout_wrap "> 
  <item name=" android:layout_width ">wrap_content</item> 
  <item name=" Android:layout_height ">wrap_content</item> 
 </style>
<!--transverse distribution-->
 <style Name= "Layout_horizontal" parent= "Layout_full" > 
  <item name= "Android:layout_width" >0px</item> 
 </style> 
<!--longitudinal distribution-->
 <style name= "layout_vertical" parent= "Layout_full" > 
  <item name= "Android:layout_height" >0px</item> 
 </style>     
</resources> 

As you can see, the Layout_width and layout_height two properties were encapsulated into 4 style

According to the actual layout, choose one of them, do not need to set their own, see my former Activitygroup demo students should be very familiar with the

And then the layout of my demo is as follows (Weight_layout.xml)

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "style=" @style/layout_full "android:orientation=" vertical "> <linearlayout style=" @style
             /layout_vertical "android:layout_weight=" 1 "android:orientation=" Horizontal "> <view style= "@style/layout_horizontal" android:background= "#aa0000" android:layout_weight= "1"/&G
         T <view style= "@style/layout_horizontal" android:background= "#00aa00" Android:layo ut_weight= "4"/> <view style= "@style/layout_horizontal" android:background= "#0000a
             A "android:layout_weight=" 3 "/> <view style=" @style/layout_horizontal " Android:background= "#aaaaaa" android:layout_weight= "2"/> </LinearLayout> <lin Earlayout Style= "@style/layout_vertical" android:layout_weight= "2" android:orientation= "vertical" > <view style= "@style/layout_vertical" android:background= "#ffffff" android:layout_weight=
             "4"/> <view style= "@style/layout_vertical" android:background= "#aa0000" android:layout_weight= "3"/> <view style= "@style/layout_vertical" Android: Background= "#00aa00" android:layout_weight= "2"/> <view style= "@style/layout_verti Cal "Android:background=" #0000aa "android:layout_weight=" 1 "/> </LinearLayout>"
 ;/linearlayout>

Second, the custom size method

This is my own idea of the method, may be a relatively stupid way, so not many people have mentioned this method to solve the problem of adaptive. Although this method has many disadvantages, it is sometimes a good method.

You can see that I've defined two sets of size files and we can look at one of the files

<?xml version= "1.0" encoding= "Utf-8"?> <resources> <dimen name= "height_1_80" >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 ">108px</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" >264px </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" > 366px</dimen><dimen name= "height_62_80" >372px</dimen> <dimen name= "height_63_80" >378px </dimen><dimen name= "height_64_80" >384px</dimen> <dimen name= "height_65_80" >390px</ Dimen><dimen name= "height_66_80" >396px</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" >426px</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 ">450px</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 values-480x320 folder under Dimens_ Height.xml the code in the file, I divide the whole height into 80 equal parts, because most screens are of 80 integers (except for special exceptions), and different dimensions are set at different resolutions.

Because each set of interfaces to write a set, so some students may not feel very good, but this writing is relatively simple, and do not have to change, so sometimes can also consider to use!

And look at the layout Code of my demo (dimen_layout.xml)

<?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" > & Lt
             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"/> <linearlayou T android:layout_width= "fill_parent" android:layout_height= "Fill_parent" Android:or    
 ientation= "Vertical" >            <button android:layout_width= "@dimen/width_30_80" android:layout_height= " @dimen/height_5_80 "android:background=" #ccffcc "android:layout_marginbottom=" @dimen/hei Ght_1_80 "android:text=" 5 "/> <button android:layout_width=" @dimen/wid
                 Th_30_80 "android:layout_height=" @dimen/height_10_80 "android:background=" #ccffcc " android:layout_marginbottom= "@dimen/height_1_80" android:text= "ten"/> <butt 
                 On android:layout_width= "@dimen/width_30_80" android:layout_height= "@dimen/height_15_80"
                 Android:background= "#ccffcc" android:layout_marginbottom= "@dimen/height_1_80"
                 android:text= "/>" <button android:layout_width= "@dimen/width_30_80" Android:layout_height= "@dimen/height_20_80" android:background= "#ccffcc" android:text= "/>"
 </LinearLayout> </LinearLayout> </LinearLayout>

The above is I write the unified layout code, everybody notices my code has margin such value also uses the custom size, if this margin uses the Layout_weight to control, certainly must more nested one layer linear layout, therefore said that does not have one method is perfect, This 2nd method is sometimes more convenient to use.

Three, set the width height in the Java code

Many people may object to this approach, because even the official recommendation is to use XML as a way to write layouts. But we're not going to be able to write that much trouble layout code like swing, because we just reset the width of the control in the code, and the other attributes are still given to the XML layout file. This method is actually stolen by me and colleagues, although I do not agree with such a method, but he is indeed a solution to the problem of screen adaptation, and it is not as complex as I imagined, in fact, very simple.

The first thing we need to do is get the width of the current screen, because this is used in the back

We can write two static variables to keep the width of the current screen high:

public class Constant {public
    static int displaywidth;//Screen width public
    static int displayheight;//Screen height
}

And then when the first activity starts, get the two values

Displaymetrics displaymetrics = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (displaymetrics);
Constant.displaywidth = Displaymetrics.widthpixels;
Constant.displayheight = Displaymetrics.heightpixels;

Layout code We can all unify written wrap-content, in fact, it doesn't matter what is written, because this value is only temporary

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "
  > <button android:id= "@+id/btn1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:background= "#ffcccc" android:text= "aaaaaaaa"/> <button android:id= "@+id/btn2" android:layout_width = "Wrap_content" android:layout_height= "wrap_content" android:background= "#ccffcc" android:text= "bbbbbbbbb"/> & Lt Button android:id= "@+id/btn3" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: Background= "#ccccff" android:text= "CCCCCCCCC"/> <button android:id= "@+id/btn4" android:layout_width= "Wrap_c" Ontent "android:layout_height=" wrap_content "android:background=" #ffffcc "android:text=" dddddddddddddddddd "/>"
 Lt;/linearlayout>

Finally we do this in the OnCreate method of the activity

First button, Width 100%, height 10% linearlayout.layoutparams params = new Linearlayout.layoutparams (layoutpar Ams.
        Fill_parent, (int) (Constant.displayheight * 0.1f + 0.5f));
        Btn1.setlayoutparams (params); The second button, width 100%, height 30% linearlayout.layoutparams params2 = new Linearlayout.layoutparams (layoutpar Ams.
        Fill_parent, (int) (Constant.displayheight * 0.3f + 0.5f));
        Btn2.setlayoutparams (PARAMS2); Third button, Width 50%, height 20% linearlayout.layoutparams params3 = new Linearlayout.layoutparams (int) (Con
        Stant.displaywidth * 0.5f + 0.5f), (int) (Constant.displayheight * 0.2f + 0.5f));
        Btn3.setlayoutparams (PARAMS3); The third button, width 70%, height fills the remaining space linearlayout.layoutparams params4 = new Linearlayout.layoutparams ((int)
        (Constant.displaywidth * 0.7f + 0.5f), layoutparams.fill_parent); Btn4.setlayoutparams (parAMS4);
 

Four, multiple layout

As a final method, and the last one to consider, that is, for different dimensions of the interface to write a separate layout. Do not have to use this method, I believe many people and I have been forced to use this method. What needs to be explained is that the horizontal screen switch using different layout is also solved by this method; code I don't.

In addition, when writing multiple layouts, the configuration file must be added to the configuration code, or it may sometimes cause problems.

<supports-screens android:largescreens= "true" android:normalscreens= "true" android:anydensity= "true"/>

V. Other

The above is a number of screens to show the same content to consider the problem, there is a different screen to display the content of different situations, in fact, this problem we are often used to solve the scrolling view, that is, scrowview; it is important to note that Scrowview is used in Layout_ Weight is invalid, since the use of Scrowview, the control inside its size are set to fixed bar.

In addition, about the image of the adaptive problem, mainly 2 points, one is 9patch, this thing we have to learn to do, not difficult; however, some compilers will have a bug like this when recognizing a 9patch map, as my eclipse would not recognize it, And the same 9patch picture on the other computer is no problem,

The second thing to say is that I have been plagued by a problem, for 480x800 and 480x854 These two dimensions, they show the same picture, there will always be a stretch (if 9patch can solve the fine). In fact, the first thing that bothers me is that these two sizes are hdpi, thinking that they can't do different pictures for these two screens. It was later discovered that pictures can be divided into multiple dimensions, not just according to the density, which means you can write folders like this drawable-hdpi-800x480 and drawable-hdpi-854x480, and put different pictures in them, So the picture can be adaptive.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.