Android:layout_weight Properties for Android LinearLayout

Source: Internet
Author: User

This article mainly introducesthe Android:layout_weight attribute meaning of Android linearlayout

Android:layout_weight is a size weight, which is equivalent to the percentage displayed on the page, and its calculation is based on this attribute value of all related elements in LinearLayout.

In addition to the fixed size, the other sets the view size (length or height) of this property to its own Layout_weight property value/All Layout_weight property value * Total size . This property is not described in the SDK for Android. The following example shows the following

For example, in a layout display 3 TextView, the first textview length of 20%, the second length of 50%, the third occupies a length of 30%,

The ratio is 20%:50%:30%=2:5:3. The layout code is as follows

Java code
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <linearlayout
  3. xmlns:android="Http://schemas.android.com/apk/res/android"
  4. Android:layout_width="Match_parent"
  5. android:layout_height="Match_parent" >
  6. <textview android:id="@+id/textview1"
  7. Android:layout_width="Wrap_content"
  8. android:layout_height="Match_parent"
  9. android:layout_weight="2"
  10. android:layout_alignparentleft="true"
  11. android:gravity="Center_vertical"
  12. android:text="Text 1"/>
  13. <textview android:id="@+id/textview2"
  14. Android:layout_width="Wrap_content"
  15. android:layout_height="Match_parent"
  16. android:layout_weight="5"
  17. android:layout_alignparentleft="true"
  18. android:gravity="Center_vertical"
  19. android:text="Text 2"/>
  20. <textview android:id="@+id/textview3"
  21. Android:layout_width="Wrap_content"
  22. android:layout_height="Match_parent"
  23. android:layout_weight="3"
  24. android:layout_alignparentleft="true"
  25. android:gravity="Center_vertical"
  26. android:text="Text 3"/>
  27. </LinearLayout>

From the above code can be seen only need to set the Android:layout_weight property value of each TextView to the corresponding scale can

where android:layout_alignparentleft= "true" android:gravity= "Center_vertical" is set for easy viewing

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.