Android screen adaptation guide (2) weight attention.

Source: Internet
Author: User

Use weight to control two buttons in horizontal direction 1:2

Horizontal direction: Layout_width = "0DP"

Using the Weight property

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" >    <ButtonAndroid:id= "@+id/button1"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Button1" />    <ButtonAndroid:id= "@+id/button2"Android:layout_width= "0DP"Android:layout_weight= "2"Android:layout_height= "Wrap_content"Android:text= "Button2" /></LinearLayout>

Change slightly: Layout_width = "Match_parent"

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" >    <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Button1" />    <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Match_parent"Android:layout_weight= "2"Android:layout_height= "Wrap_content"Android:text= "Button2" /></LinearLayout>

Comparison of the two effects

Only changes the layout value, resulting in two different display types.

How the actual width of the control is calculated:

width = percent of original width + remaining width;

In layman's terms:

width = layout_weight defined width + (screen width-width of two components layout_weight) * Percentage

Assuming the screen width is L

for layout_width = "0DP "

Button1: = 0DP + (L-0DP-0DP) * 1/3 = 1/3l

Button2: = 0DP + (L-0DP-0DP) * 2/3 = 2/3l

Layout_width= "Match_parent", the original width is L

Button1 = L + (L-(l+l)) *1/3 = l-1/3l = 2/3l

Button2 = L + (L-(l+l)) *2/3 = l-2/3l = 1/3l

Android screen adaptation guide (2) weight attention.

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.