對自己現在所瞭解的layout_weight屬性進行記錄,不求全面,隻求正確!
layout_weight意為"權重",我的理解就是給組件設置一個顯示大小的比例。
layout_weight設置一個值,會出現兩種情況。
第一種:當組件的“layout_width”屬性為“fill_parent”時,值越小,組件越大。
第二種:當組件的“layout_width”屬性為“wrap_content”時,值越大,組件越大。
第一種情況:
Xml代碼
<LinearLayout
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<Button
android:id="@+id/btn_save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="保存"
/>
<Button
android:id="@+id/btn_return"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="返回"
/>
</LinearLayout>
在這裡"保存"按鈕的Layout_weight=1,"返回"按鈕的Layout_weight=4,layout_width="fill_parent"時, 運行效果為:
第二種情況:
Xml代碼
<LinearLayout
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="保存"
/>
<Button
android:id="@+id/btn_return"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="返回"
/>
</LinearLayout>
在這裡"保存"按鈕的Layout_weight=1,"返回"按鈕的Layout_weight=4,layout_width="wrap_content"時,運行效果為:
以上為本人對已知情況的總結,如有不對或總結不足,望指教!
作者“背著行囊獨自前行….”