理解weight和layout_weight,layout_weight越大,它在布局中收缩得越多。

3

我正在尝试通过这个示例理解权重布局。

这绝对不是什么高深的东西。然而,这个例子让它变得更加容易理解...

  • weightSum决定了布局的大小
  • 然后基于LinearLayout中视图的layout_weight值对布局进行划分。

在这个例子中,我有一个带有权重为5的布局,然后将其在两个视图之间划分:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/transactionRowBackground"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" android:layout_weight="2" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="top"
            android:padding="5dp"
            android:text="Test Title"
            android:textColor="@color/textColor"
            android:textSize="@dimen/subHeadingTextSize"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/description"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="top"
            android:padding="5dp"
            android:text="This is a test description"
            android:textColor="@color/textColor"
            android:textSize="@dimen/normalTextSize" />

    </LinearLayout>


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="top"
        android:contentDescription="" />

</LinearLayout>

我无法理解的是,我给ImageViewer的数字越大,它从父级获取的空间就越小。那么它实际上是如何计算ImageView的大小的呢?
您可以尝试使用上面的xml。如果将ImageView的布局权重更改为1,子线性布局更改为4,则我认为这更有意义,然后相反的情况将发生。
ImageView将扩展,而子线性布局将缩小。我以为数字越大,你就会得到更多的空间。
2个回答

3

因为你的最外层布局中有android:orientation="horizontal",我认为你希望在水平方向上改变 ImageView 和内部 LinearLayout 所占用的大小/空间。对于这种情况,请尝试使用

android:layout_width="0dp"

在你使用了android:layout_weight的布局中,如果外层布局的方向是垂直的,我会使用android:layout_height="0dp"来处理布局的宽度/高度。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/transactionRowBackground"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical" android:layout_weight="2" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="top"
            android:padding="5dp"
            android:text="Test Title"
            android:textColor="@color/textColor"
            android:textSize="@dimen/subHeadingTextSize"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/description"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:gravity="top"
            android:padding="5dp"
            android:text="This is a test description"
            android:textColor="@color/textColor"
            android:textSize="@dimen/normalTextSize" />

    </LinearLayout>


    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="top"
        android:contentDescription="" />

</LinearLayout>

阅读Android文档可能会有所帮助:布局权重


谢谢!我忘记了一个规则,即一旦您设置了权重,您必须将宽度设置为0dp。否则,该权重会被忽略。 - codebased

1
使用layout_weight可为多个视图指定大小比例。例如,您有一个MapView和一个表格,表格应显示地图的一些其他信息。地图应使用屏幕的3/4,表格应使用屏幕的1/4。然后,您将设置地图的layout_weight为3,表格的layout_weight为1。
要使其正常工作,您还必须将高度或宽度(取决于方向)设置为0dp示例 如果有三个文本框,其中两个声明了权重为1,而第三个未给出权重(0),则剩余空间分配如下:

第一个文本框= 1 /(1 + 1 + 0)

第二个文本框= 1 /(1 + 1 + 0)

第三个文本框= 0 /(1 + 1 + 0)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接