501 Android Development Tips (01 Take advantage of the Layout_weight property)

Source: Internet
Author: User

question: How do I place a button in the middle of the layout and set its width to the 50% of the parent?
Analysis: The effect that the problem wants to achieve should be this:
(Original address: http://blog.csdn.net/vector_yi/article/details/24397733)

This may not seem difficult, but many developers do not know the best way to achieve this effect.


WORKAROUND: Here we use the Weightsum property with the Layout_weight property.


<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" ><!--1. Adding Android:weightsum properties    -- <button        android:layout_width = "0DP" <!--2. Set the layout_width of the Button to        0dp--> android:layout_height = " Wrap_content "        android:layout_weight =" 0.5 "<!--3. Make sure it takes up 50% of the free space--        Android:text =" @string/activity _main_click_me "/></linearlayout>
Note that in the 2nd step, the layout_width of the button is set to 0DP. Will there be a conflict with layout_weight? The answer is no: The width of a control is calculated like this:   Widget ' s width + widget ' s Weight*parent's width/parent s weightSum

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

501 Android Development Tips (01 Take advantage of the 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.