有的时候我们开发一个产品的时候需要让其中某个控件的宽度或高度占据其父容器的宽度或高度的一半显示,这个时候由于设备尺寸的限制,做到在每个设备上都具有同样的效果的话,我们就需要用到weightSum属性和layout_weight属性。
具体的实现过程分析如下:
首先为父容器指定一个weightSum,然后为其子控件的layout_weight属性值设为weightSum的一半,这个时候,就实现了占据其一半的效果。
<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>