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
- <?xml version="1.0" encoding="Utf-8"?>
- <linearlayout
- xmlns:android="Http://schemas.android.com/apk/res/android"
- Android:layout_width="Match_parent"
- android:layout_height="Match_parent" >
- <textview android:id="@+id/textview1"
- Android:layout_width="Wrap_content"
- android:layout_height="Match_parent"
- android:layout_weight="2"
- android:layout_alignparentleft="true"
- android:gravity="Center_vertical"
- android:text="Text 1"/>
- <textview android:id="@+id/textview2"
- Android:layout_width="Wrap_content"
- android:layout_height="Match_parent"
- android:layout_weight="5"
- android:layout_alignparentleft="true"
- android:gravity="Center_vertical"
- android:text="Text 2"/>
- <textview android:id="@+id/textview3"
- Android:layout_width="Wrap_content"
- android:layout_height="Match_parent"
- android:layout_weight="3"
- android:layout_alignparentleft="true"
- android:gravity="Center_vertical"
- android:text="Text 3"/>
- </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