How to use Android:layout_weight in Android

Source: Internet
Author: User

When using LinearLayout, the child controls can set the Layout_weight. The role of Layout_weight is to set the importance of the subspace in LinearLayout (the size of the control). The lower the value of the layout_weight, the more important the control is. If you do not set Layout_weight, the default weighting is 0.

If you place two Button,button1 and Button2,button1 Layout_weight in a linearlayout set to 1,button2 layout_weight setting to 2, And the layout_width of the two button are set to Fill_parent.

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal" >
<button
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "Button1"/>
<button
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_weight= "2"
android:text= "Button2"/>
</LinearLayout>

The Button1 occupies two-thirds of the screen width, while Button2 occupies One-third, as shown in:

If the layout_width of the two button are set to Wrap_content, the situation is reversed. Button1 accounted for One-third and Button2 accounted for Two-thirds as shown:

Ayout_weight is useful when designing complex layouts with linearlayout, for example, in a horizontal linear layout where you have enough space for control 1, and the rest of the space is assigned to control 2, as long as you set the layout_ of control 1 Width is set to wrap_content without setting layout_weight, whereas in control 2, setting Layout_width to Fill_parent,layout_weight is 1 .

This article goes from http://www.jb51.net/article/38560.htm, thanks to the author for sharing

How to use Android:layout_weight in Android

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.