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)