layout_wlayout_weight表示该View控件占据父控件剩余空间的比例
代码示例:
1.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#0000FF"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<LinearLayout
android:background="#00FF00"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
未使用layout_weight,第一个linearLayout将父控件全部覆盖,无法显示第二个。
如果android:layout_height="wrap_content"android:layout_width="wrap_content"将导致两个linearLayout没有内容,显示在角落,无法看见。
2.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#0000FF"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1" />
<LinearLayout
android:background="#00FF00"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
示例一
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#0000FF"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="2" />
<LinearLayout
android:background="#00FF00"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
示例二
对比示例一二可以很好的理解layout_weight的定义,即表示占据父控件剩余空间的比例;
示例一中,两个子线性布局的layout_weight均为1,所以每一个布局占据父控件剩余空间的1/(1+1)=0.5;
实例二中,LinearLayout1的layout_weight为2,LinearLayout2的layout_weight为1,所以LinearLayout1占据父控件的比例是2/3,LinearLayout2占据父控件的比例是1/3;
3.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:text="small"
android:layout_width="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_height="wrap_content"
android:text="A very very long text that needs to wrap."
android:layout_width="wrap_content"
android:layout_weight="1" />
</LinearLayout>
实例三:注意TextView1和2均为android:layout_width="wrap_content"。
因为TextView1和2根据weight属性分别占据父控件的1/2,所以宽度应该相同。但是结果并不是这样,TextView1宽度明显小于TextView2。
这是因为layout_weight表示的并不是占据父控件的比例,而是父控件剩余空间的比例。TextView1和TextView2内容填充完后,并没有完全布满父控件的宽度,还有剩余空间,即图中所示。根据layout_weight属性,两者分别占据了剩余空间的1/2.
TextView1和TextView2的宽度并不相同,但是两个TextView除了内容外的空余部分确实相同的。
通过对比实例三和示例四,相信可以很好地理解剩余空间比例的问题。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:text="small"
android:layout_width="0dp"
android:layout_weight="1" />
<TextView
android:layout_height="wrap_content"
android:text="A very very long text that needs to wrap."
android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
实例四
4.weightSum
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:weightSum="4"
android:padding="5dp"> <!-- to show what the parent is -->
<LinearLayout
android:background="#0000FF"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="2" />
<LinearLayout
android:background="#00FF00"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
在上面的示例中,父控件的weight根据将子控件的weight相加得到,所以父控件的空间被子控件完全填充;
使用weightSum,可以设置父控件的weight属性多于子控件,这样父控件不会完全被占满,其中一部分显示父控件的内容。
原英文作者网址http://www.chess-ix.com/blog/the-use-of-layout_weight-with-android-layouts/
作者的示例很好的体现了layout_weight的用法和意义,所以照搬过来。
解释部分是按照原文和自己的实践理解写的,有错误还望指出!