Set a half screen size and center button layout in Android (Layout_weight property)

Source: Internet
Author: User

look first for example the following layout:


In The size of the button is half the screen, then centered in the center of the layout, each person has its own answer, see my method, layout layout XML such as the following:

<?

XML version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android " android:id=" @+id/main_layout " android:layout_width=" match_parent " android:layout_height=" Match_ Parent " android:baselinealigned=" false " android:gravity=" center " android:orientation=" Horizontal " android:weightsum= "1" > <!--1. Set LinearLayout layout_weight to 1--and <!--2. The Layout_ of the button Width is set to 0DP, and Layout_weight is set to 0.5- <button android:layout_width= "0DP" android:layout_ height= "Wrap_content" android:layout_weight= "0.5" android:textsize= "13sp" android:text= "@string /button_text "> </Button></LinearLayout>

Here are a few key points:

    1. The layout is linearlayout so that layout_weight can be set.
    2. Set the value of the LinearLayout android:weightsum to 1;
    3. Set the button's layout_width to 0DP;
    4. Set the value of the layout_weight of the button to 0.5. Which is half the android:weightsum above.
you can implement the layout in the above settings.

The width of the subspace algorithm can be used for example the following formula, child control width = child control layout_width + child control Layout_weight * Parent control's width/parent control's layout_weight, compared to the above example is the child control width = 0.5 * The parent control's Width/1 = parent control width * 0.5. Because the width of the parent control is match_parent, the width of the parent control width is the width of the entire screen, so the width of the child control = 0.5 * Width of the entire screen = half screen width.



Set a half screen size and center button layout in Android (Layout_weight property)

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.