LinearLayout的layout_weight属性与视图分配

来源:互联网 发布:网络主播收入排行 编辑:程序博客网 时间:2024/06/05 05:18

LinearLayout的layout_weight属性与视图分配

在Android中,LinearLayout几乎是使用得最多的一种布局。
而在LinearLayout中,layout_weight属性又是很多同学没有理解的。

android:layout_weight属性用来进行LinearLayout的组件布局安排。如果我们为同一个LinearLayout中的两个组件设置了相同的android:layout_weight值,这并不意味着他们在屏幕中有相同的高度或者宽度(依布局方向而定)。

要理解这些,首先要清楚,在LinearLayout中,视图宽度是分成两个步骤来分配的。

第一步, LinearLayout查看layout_width或者layout_height的值(依布局方向而定),一个很简单的例子,在一个LinearLayout中,有两个组件,当这两个组件的layout_width都设置为wrap_content时,他们获得的空间仅能绘制自身。代码如下。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="left"    android:orientation="horizontal">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="30sp"        android:text="@string/hello_world" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></LinearLayout>

实际效果如下。

实际效果如下

可以看到整个横部还有剩余的空间。

第二步,LinearLayout依据layout_weight属性进行额外的空间分配。
如果我们给他们赋以相同的layout_weight属性值,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="left"    android:orientation="horizontal">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="30sp"        android:layout_weight="1"        android:text="@string/hello_world" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="@string/hello_world" /></LinearLayout>

而效果是这样的:
效果2

可以看到,虽然两个的layout_weight属性值都一样,但是他们分配到的空间却是不一样的,原因是LinearLayout在第一步已经分配过空间,第二步只是将剩余的空间分配。事实上,第一步中剩余的空间,在这一步中被平均分配了。


那么问题来了,如果我想这两个控件平均分配整个空间而不是剩余空间,该怎么做?
很简单,取消掉LinearLayout的第一步空间分配,只进行第二步分配。

  • 1:1分配
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="left"    android:orientation="horizontal">    <TextView        android:layout_width="0dp"        android:layout_height="wrap_content"        android:textSize="30sp"        android:layout_weight="1"        android:text="@string/hello_world" />    <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="@string/hello_world" /></LinearLayout>

效果3

  • 1:2分配
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="left"    android:orientation="horizontal">    <TextView        android:layout_width="0dp"        android:layout_height="wrap_content"        android:textSize="20sp"        android:layout_weight="1"        android:text="@string/hello_world" />    <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="2"        android:text="@string/hello_world" /></LinearLayout>

效果4

可以看到,如果将layout_width属性值(垂直布局时是layout_height)设置为0dp,那么就可以跳过LinearLayout的第一步空间分配,从而轻松实现一定的宽度比。

0 0