Personal Technology Blog-android Layout tips

Source: Internet
Author: User

(1) Rational Application of weight attribute

When using Match_parent (fill_parent), it needs to be calculated, otherwise the following conditions will occur
Code:

  <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http/ Schemas.android.com/tools "android:id=" @+id/linearlayout1 "android:layout_width=" Match_parent "Android:layout_hei        ght= "Match_parent" > <textview android:layout_weight= "1" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" android:text= "one" android:background= "#98FB98"/> <text         View android:layout_weight= "2" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" android:text= "android:background=" #FFFF00 "/> <textview android:layout_weight="        3 "android:layout_width=" Fill_parent "android:layout_height=" Fill_parent "android:text=" three " Android:background= "#FF00FF"/></linearlayout>  

The effect is as follows

This time found a problem, three disappeared? There is three in the code, but also set 3, and 1 and 2 of the proportion is not right, 1:2:3 into 2:1:0, why so? After consulting the data I found that it is not so simple, this is needed to calculate, the online algorithm has several step 1: all are fill_parent, But the screen is only one, then 1-3 = 2 fill_parent Step 2: The ratio is 1/6,2/6,3/6 Step 3: First come first served first, calculate: 1-2 * (1/6) = 2/3 fill_parent then to the second, count Count: 1-2 * (2/6) = 1/3 Fill_parent Last to three, calculation 1-2 * (3/6) = 0 fill_parent Step 4: So the final result: one accounted for two, two accounted for, and three had nothing. That's why three didn't show up.

If the ratio is 1:1:1, the effect is as follows

Calculated as above, the result is: 1/3 1/3 1/3, yes.

If the ratio is 2:3:4, the effect is as follows

Calculation results: 5/9 3/9 1/9, contrast, 5:3:1, that's right.

(2) The difference between margin and padding

For these two properties may be a bit confusing, first margin represents an offset, such as MarginLeft = "5DP" means that the component is offset from the left edge of the container 5DP; While padding represents the fill, and the filled object is for elements in the component, such as TextView in the text such as TextView set paddingleft = "5DP", then the element in the component of the left padding 5DP space. Margin is for the components in the container, and padding is for the elements in the component. Here's a simple code to demonstrate the difference:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "a        ndroid:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin"        android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" Tools:context= ". Mainactivity "> <button android:id=" @+id/btn1 "android:layout_height=" wrap_content "Android:layout_width=" wrap_content "android:text=" button "/> <button androi            d:paddingleft= "100DP" android:layout_height= "wrap_content" android:layout_width= "Wrap_content" android:text= "button" android:layout_torightof= "@id/btn1"/> <button Android  Oid:id= "@+id/btn2"           android:layout_height= "Wrap_content" android:layout_width= "wrap_content" android:text= "Bu Tton "android:layout_alignparentbottom=" true "/> <button android:layout_marginleft=" 100d P "android:layout_height=" Wrap_content "android:layout_width=" Wrap_content "Android:text            = "button" android:layout_torightof= "@id/btn2" android:layout_alignparentbottom= "true"/>  </RelativeLayout>

The effect is as follows

In addition, margin can be set to negative, such as after entering the software, pop-up ads page, the top right corner of the cancle button margin is used negative, the code is as follows

    <ImageView          android:id="@+id/imgBack"          android:layout_width="200dp"          android:layout_height="200dp"          android:layout_centerInParent="true"          android:background="@drawable/myicon" />        <ImageView          android:id="@+id/imgCancle"          android:layout_width="28dp"          android:layout_height="28dp"          android:layout_alignRight="@id/imgBack"          android:layout_alignTop="@id/imgBack"          android:background="@drawable/cancel"          android:layout_marginTop="-15dp"  //cancle按钮的margin使用负数        android:layout_marginRight="-10dp" />  

This is my Android layout summed up in some of the tips, relatively low, only for reference ha ha ~

Personal Technology Blog-android Layout tips

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.