android:layout_weight详解

来源:互联网 发布:php webservice 编辑:程序博客网 时间:2024/06/08 16:05

LinearLayout中支持使用android:layout_weight属性为各个子视图分配权重,权重值更大的视图可以填充父视图中任何剩余的空间。
官方介绍:https://developer.android.com/guide/topics/ui/layout/linear.html

1、简单举例

需求:水平布局中的三个TextView的宽度比为1:2:3
需求样式
代码:
将android:layout_width都设为0dp,然后将三个视图的android:layout_weight设为1/2/3即可。

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal">    <TextView        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:gravity="center"        android:text="1"        android:background="#44f21515"/>    <TextView        android:layout_width="0dp"        android:layout_weight="2"        android:layout_height="wrap_content"        android:gravity="center"        android:text="2"        android:background="#4408e72d"/>    <TextView        android:layout_width="0dp"        android:layout_weight="3"        android:layout_height="wrap_content"        android:gravity="center"        android:text="3"        android:background="#44292ce1"/></LinearLayout>

2、进阶

从引言的谷歌官方描述中,注意这句话“权重值更大的视图可以填充父视图中任何剩余的空间”中的“剩余”二字,这是使用android:layout_weight所需重点关注的。
也就是:
LinearLayout中的layout_weight属性,首先按照控件声明的尺寸进行分配,然后再将剩下的尺寸按weight分配

2.1 举例1

修改简单例子中的第一个TextView,使其android:layout_width=”wrap_content”,android:text=”11111111111111”,删去android:layout_weight=”1”,效果如下:
举例1
可以看到,第二第三个TextView以2:3的比例填充了总空间中去除了第一个TextView使用后剩余的空间。
总得代码如下:

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="center"        android:text="11111111111111"        android:background="#44f21515"/>    <TextView        android:layout_width="0dp"        android:layout_weight="2"        android:layout_height="wrap_content"        android:gravity="center"        android:text="2"        android:background="#4408e72d"/>    <TextView        android:layout_width="0dp"        android:layout_weight="3"        android:layout_height="wrap_content"        android:gravity="center"        android:text="3"        android:background="#44292ce1"/></LinearLayout>

2.2 举例2

那如果不删掉第一个TextView的android:layout_weight=”1”呢,效果如下:
举例2
可以看到,第一个TextView也会额外去占用1/(1+2+3)的剩余空间。

3、其他技巧

如果想达到如下效果:(只有一个TextView,占据LinearLayout一半的空间)
其他技巧
可以利用LinearLayout的android:weightSum属性,指定这个LinearLayout总的权重值
代码:

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    android:weightSum="2">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:gravity="center"        android:text="1"        android:background="#44f21515"/></LinearLayout>

以上就是我对android:layout_weight的一点理解,欢迎交流

0 0