有的时候我们开发一个产品的时候需要让其中某个控件的宽度或高度占据其父容器的宽度或高度的一半显示,这个时候由于设备尺寸的限制,做到在每个设备上都具有同样的效果的话,我们就需要用到weightSum属性和layout_weight属性。
具体的实现过程分析如下:
首先为父容器指定一个weightSum,然后为其子控件的layout_weight属性值设为weightSum的一半,这个时候,就实现了占据其一半的效果。
1 < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" 2 android:layout_width ="fill_parent" 3 android:layout_height ="fill_parent" 4 android:background ="#ffffff" 5 android:gravity ="center" 6 android:orientation ="horizontal" 7 android:weightSum ="1" > 8 9 < Button 10 android:layout_width ="0dp" 11 android:layout_height ="wrap_content" 12 android:layout_weight ="0.5" 13 android:text ="@string/activity_main_click_me" /> 14 15 </ LinearLayout >