四十四、Android之android:layout_weight详解
1、LinearLayout可以为其包含控件指定填充权值layout_weight。 这样就允许其包含的控件可以填充屏幕上的剩余空间。这也避免了所有控件挤成一堆的情况,而是允许他们放大填充所有空白。剩余的空间会按这些控件指定的权值比例分配屏幕。
2、默认情况下,weight的值是0,表示按照控件的实际大小显示;如果weight设置高于零。
3、剩余空间会按照该控件的weight值占所有控件weight的比例分配给该控件。 比如有两个控件,一个weight为1,另外一个是2. 则剩余空间会把1/(1+2)的部分给控件一,另外2/(1+2)的分配给控件二。也就是权值越大,重要度越大。
4、如果LinearLayout包含子LinearLayout,子LinearLayout之间的权值越大的,重要度则越小。如果有LinearLayout A包含LinearLayout C,D,C的权值为2,D的权值为1,则屏幕的2/3空间分给权值为1的D,1/3分给权值为2的C。在LinearLayout嵌套的情况下,子LinearLayout必须要设置权值,否则默认的情况是未设置权值的子LinearLayout占据整个屏幕。
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:orientation=
"vertical"
>
<LinearLayout
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:orientation=
"vertical"
android:layout_weight=
"2"
>
<SurfaceView
android:id=
"@+id/surfaceView"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
/>
</LinearLayout>
<LinearLayout
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:orientation=
"horizontal"
android:layout_weight=
"1"
android:layout_gravity=
"center"
>
<Button android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:id=
"@+id/btnTakePicture"
android:gravity=
"center"
android:textSize=
"20px"
android:text=
"拍照"
/>
</LinearLayout>
</LinearLayout>
|