layout_weight 属性分析

以如下布局为例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <Button android:layout_width="fill_parent" android:text="@string/bt_show1"
        android:textColor="#ff0000"
            android:layout_height="wrap_content" android:layout_weight="10.0" />

        <Button android:layout_width="fill_parent" android:text="@string/bt_show2"
        android:textColor="#ff0000"
            android:layout_height="wrap_content" android:layout_weight="1.0" />

    </LinearLayout>


    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <Button android:layout_width="wrap_content" android:text="@string/bt_show3"
        android:textColor="#ff0000"
            android:layout_height="wrap_content" android:layout_weight="10.0" />

        <Button android:layout_width="wrap_content" android:text="@string/bt_show4"
        android:textColor="#ff0000"
            android:layout_height="wrap_content" android:layout_weight="1.0" />

    </LinearLayout>

</LinearLayout>

可以看作:

1、全为wrap_content 时 layout_weight月接近0,越接近wrap_content(占比例越小)

      当layout_weight=0 时 ,width 为 wrap_content; 如果全部layout_weight =0 ,全部为wrap_content,而并不会和全为其他非零值时一样平均分割;

2、全为fill_parent 时layout_weight越接近0,越接近fill_parent(占比例越大)

     有多个layout_weight为0时,第一个为0的充满父控件

3、layout_weight 为负数则不在视图内

原文地址:https://www.cnblogs.com/lipeil/p/2666883.html