Android: the larger layout_weight, the larger the proportion, and the smaller the proportion,
Abstract: My technical blog is often maliciously crawled and reproduced by rogue websites. Please move to the original article: Workshop.
For android: What does layout_weight mean? There are two ways on the Internet: "The larger the proportion, the larger the proportion, and" the smaller the proportion. In fact, both cases also exist.
Example 1: 0dp
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="fill_parent" android:layout_height="0dp" android:background="#ff0000" android:layout_weight="1" android:text="1"/> <TextView android:layout_width="fill_parent" android:layout_height="0dp" android:background="#00ff00" android:layout_weight="2" android:text="2"/></LinearLayout>
Example 2:
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#ff0000" android:layout_weight="1" android:text="1"/> <TextView android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#00ff00" android:layout_weight="2" android:text="2"/></LinearLayout>
Two cases:
The layout first allocates the width to each element, and the remaining space is allocated to each element.
In the remaining space allocation phase, the ratio is based on layout_weight, that is, the larger the proportion, the larger the proportion.
In Case 1, the fixed width is 0, and only the second stage is used. In case 2, the allocated width can fully split all spaces, and the remaining space is negative. Therefore, the larger the allocation, the smaller the final space.
So if you want to strictly set it to a certain proportion, you can set it to 0dp. In this case, the proportion of each element is weight.
The sub-element attribute android: layout_weight in LinearLayout has other features.
I don't know what you are asking. I currently use this property. For example, one row has two controls, and each control
Android: layout_weight = "1" and they will divide the region equally.
For example, if you have a TextView, the following ListView only needs to set android: layout_weight = "1" for the listview to make the textview not covered during the listview drop-down.
Android programming android: layout_weight = "1" error "Invalid layout param in a RelativeLayout: layout_weight
Android: layout_weight is only applicable to linear layout and not RelativeLayout.