"Android Development Note" Weight properties

Source: Internet
Author: User

Mixed use of weightsum and Layout_weight

Look at the effect first, the button occupies half the width of the screen.


Look again at the description in the development documentation.

"Defines the maximum value for the sum of weight. If this value is not specified, the cumulative value of the Layout_weight property of all child views is used as the maximum value of the sum. A typical case is: by specifying the Layout_weight property of the child view to 0.5, and setting the LinearLayout's Weightsum property to 1.0, the implementation child view occupies 50 of the available width. ”

The source code of the XML file.

<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 "        android:layout_height=" wrap_content "        android:layout_weight=" 0.5 "        android:text=" @string/ Activity_main_click_me "/></linearlayout>
The Android:layout_width property of the specified button is 0DP, so the width of the button needs to be determined based on the Android:weightsum property.

Assume that there is a width of 200dp,android:weightsum property that is 1.0 linearlayout. The formula for the button width in this linearlayout is as follows.

Button ' s width + button ' s weight * 200/sum (weight)
Specifies that the button has a width of 0dp,weight of 0.5,sum (weight) equal to 1, then the result is as follows.

0 + 0.5 * 200/1 = 100


Profile

Using the LinearLayout weight property is necessary when you need to allocate the available space on a proportional basis, which avoids the side effects of hard-coded methods. If the target platform is honeycomb and uses fragment, then most of the cases are using weight to go to the layout file and give you a space for the fragment allocation. An in-depth understanding of how to use weight will add an important skill to the developer.


External links

Http://developer.android.com/reference/android/widget/LinearLayout.html

Http://mobile.51cto.com/abased-375428.htm

"Android Development Note" Weight properties

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.