理解weight和layout_weight,layout_weight越大,在layout中缩小的越多。

Understanding weight and layout_weight, the bigger layout_weight is, more it shrinks in the layout.

我试图通过这个例子来理解权重布局。

这绝对不是火箭科学。然而,这个例子使它...

在这个例子中,我有一个布局,权重为 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 的更大的数字是它从父级获得的最小 space。那么它实际上是如何计算 ImageView 的大小的。

你可以试试上面的xml。如果您将 ImageView 的布局权重更改为 1 ,并将子线性布局更改为 4,我认为这更有意义,那么将发生相反的情况。

ImageView 会扩大,子线性布局会缩小。我以为数字越大你得到的越多 space.

由于在最外层的布局上有 android:orientation="horizontal",我相信您想在水平方向上改变 ImageView 和内部 LinearLayout 所采用的 size/space。对于这种情况,请尝试使用

android:layout_width="0dp"

在您放置 android:layout_weight 的布局上。如果您的外部布局方向是垂直的,我会使用 android:layout_height="0dp" 以便权重处理布局的 width/height。

<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 文档可能会有帮助:Layout Weights

使用 layout_weight 您可以指定多个视图之间的大小比例。例如。您有一个 MapView 和一个 table,它们应该向地图显示一些附加信息。地图应使用屏幕的 3/4,table 应使用屏幕的 1/4。然后你将地图的 layout_weight 设置为 3,将 table 的 layout_weight 设置为 1.

要使其正常工作,您还必须将高度或宽度(取决于您的方向)设置为 0dp示例

如果有三个文本框,其中两个声明权重为 1,而第三个没有赋予权重 (0),则剩余 space 分配如下:

1st text box = 1/(1+1+0)

2nd text box = 1/(1+1+0)

3rd text box = 0/(1+1+0)