Android layout Learning -- layout_weight attribute of LinearLayout
I have been confused about the layout_weight attribute. I want to learn more about the layout_weight attribute and its usage today. Definition first, let's look at what the official Android documentation says. After all, people are authoritative. The official document indicates that the layout_weight attribute is used to allocate extra space (extra space) in LinearLayout ). If the View does not want to be stretched, set layout_weight to 0. Otherwise, these pixels will be allocated to all views whose weight value is greater than 0. In other words, the android: layout_weight attribute tells LinearLayout how to arrange child components. If there are so many examples, it is better to use several examples to describe it in an image: 1. First, set a Linear_Layout layout, set the direction to horizontal, place two textviews, and do not set the Layout_weight value. The white space is the extra space mentioned in the official document ). Copy code 1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "wrap_content" 5 android: orientation = "horizontal"> 6 7 <TextView 8 android: layout_width = "wrap_content" 9 android: layout_height = "wrap_content" 10 android: background = "# f00" 11 android: text = "TextView1"/> 12 <TextV Iew 13 android: layout_width = "wrap_content" 14 android: layout_height = "wrap_content" 15 android: background = "#0f0" 16 android: text = "ThisIsTextView2" 17/> 18 </LinearLayout> 2. set the value of Layout_weight for both textviews to 1. In the preceding xml file, add the "android: layout_weight = 1" attribute to each TextView. The layout_weight values of TextView1 and TextView2 are set to 2, and 2 respectively to see the running effect: I believe that through the above examples and images, you should have a good understanding of the layout_weight attribute usage. Then the problem comes again. If I want to allocate the two child components with the same width or height, how can I set layout_weight? You only need to set the layout_width value of each sub-component to 0 to avoid space allocation in the first step. In this way, LinearLayout will only consider using the layout_weight value for space allocation. Copy code 1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "wrap_content" 5 android: orientation = "horizontal"> 6 7 <TextView 8 android: layout_width = "0dp" <! -- Set layout_width to 0dp to avoid space allocation in step 1 --> 9 android: layout_height = "wrap_content" 10 android: background = "# f00" 11 android: layout_weight = "1" <! -- LinearLayout allocates space based on this value --> 12 android: text = "TextView1"/> 13 <TextView 14 android: layout_width = "0dp" 15 android: layout_height = "wrap_content" 16 android: background = "#0f0" 17 android: layout_weight = "1" 18 android: text = "ThisIsTextView2" 19/> 20 </LinearLayout>