Android: the larger layout_weight, the larger the proportion, and the smaller the proportion,

Source: Internet
Author: User

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.

Related Article

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.