四十四、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
        xmlns:android="http://schemas.android.com/apk/res/android"
        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
        xmlns:android="http://schemas.android.com/apk/res/android"
        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>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM