Android layout learning-layout_weight attribute of LinearLayout and androidlayout

Source: Internet
Author: User

Android layout learning-layout_weight attribute of LinearLayout and androidlayout

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 documents mean:

The layout_weight attribute is used to allocateExtra space (extra space ).

If the View does not want to be stretched, set layout_weight to 0. Otherwise, these pixels will be allocated proportionally

All views with these weight values greater than 0.

In other words, the android: layout_weight attribute tells LinearLayout how to arrange child components.

  •  Example

To describe it with so many examples:

1. First, set a Linear_Layout layout, set the direction to horizontal, place two textviews, and do not set the Layout_weight value. We can see that

The white space is the extra space mentioned in the official document ).

Layout file 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     <TextView 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, 1, 1, and 2 respectively to see the running effect:

2 and 1:

1 and 2:

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?

OnlySet the layout_width value of each sub-component to 0.In this way, the space allocation in the first step is avoided.

In this way, LinearLayout will only consider using the layout_weight value for space allocation.

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>

I hope this article will help you. If you like it, please recommend it. Thank you ~

If you repost this blog, enter http: www.cnblogs.com/JohnTsai at the beginning of the article.

Welcome to discussion, email: JohnTsai.Work@gmail.com


Android: layout_weight in the LinearLayout Layout

Learn from you !~~ This test is thorough enough !~~ Hope you can find the answer !~~

Why can the android layout_weight attribute display controls in a certain proportion?

Layout_weight is the weight attribute of the component in the layout. A larger value indicates more parts of the site.

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.