Reading notes _ "Android Hacks" one of LinearLayout's weightsum and weights

Source: Internet
Author: User

Recently read "Android Hacks", ready to thank reading notes, and constantly plump.

Heard the question, "What should I do if I want a button to account for 50% of the parent control".

In general, we can use the LinearLayout property Android:layout_weight property

There are several ways to implement this approach.

Android devices have different size, for different screen sizes, we should have a common

The applicable method.

We can use the Layout_weight and Weightsum properties to fill the remaining space of the layout.

Where android:weightsum defines the maximum value of a weight. If the Weightsum property

As defined, it is equal to the sum of the weight of all the child controls it contains. For the above question,

You can set Layout_weight to 0.5 for the unique character space in the control, and set Weightsum to 1.0

Imagine that we are going to place items in a box. The free space of the box is Weightsum,

The corresponding Layout_weight attribute indicates the space occupied by the items in the box. To give a simple example,

The entire box has a weightsum of 1, with two controls, A and B, where A is layout_weight

The layout_weight of 0.25,b is 0.75. So control A has 25% of the space, and control B has 75%

Space.

So we go back to the beginning of the problem, we give the weightsum of the parent control, and then give the child control the

The button whose property android:layout_weight takes half its value. Where the XML attributes are

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

Android:background= "#ffffff"

android:gravity= "Center"

android:orientation= "Horizontal"

android:weightsum= "1" >

<button

Android:layout_width= "0DP"//Gets the width of the button

android:layout_height= "Wrap_content"

android:layout_weight= "0.5"//occupy 50% of the remaining space

android:text= "click Me"/>

</LinearLayout>

LinearLayout gets the Android:weightsum property and specifies that the sum of weight for all child controls is 1.

Then, because 1,button is the only child control. 2. Set the width to 0DP and then based on the Weigth property 0.5

So the entire button occupies 50% of the parent control.

So if the width of the linearlayout is 200DP, its properties are android:weightsum=1. Then there will be the following formula

Button Width+button weight*200/sum (weight).

Corresponds to a specific instance of 0dp+0.5*200/1=100

Reading notes _ "Android Hacks" one of LinearLayout's weightsum and weights

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.