Android - 使用 layout_weight 时指定 layout_width

Android - specifying layout_width when using layout_weight

当我在 say 和 image view 之间指定 weight 和包含在同一个 linear layout 中的 linear layout 时,我应该设置什么 layout_width 价值观?似乎不应该设置它,因为我是说我想让另一个 layout 占据 space 的 90%,image view 占据 space 的 10%。

我似乎还可以将 image view 设置为 10% space 并将 layout 设置为 90% space 但我仍然可以指定一个值在 dp'simage view?

在这种情况下,我应该只在 image 上设置 dp's 中的值,然后为另一个 layout 指定一个 1weight

谢谢

在这种情况下,您的 layout_width 应该是 0dp。如果使用重量参数定义 0dp 以外的宽度,您还会收到 lint 警告。

refer this了解更多详情。

在布局中使用权重时。 你会想要这样做的。

parent 布局将有一个 weightSum=10 使用值 10,但它可以是我们想要的任何值。 对于 children 布局。全部应该是layout_width="fill_parent"然后你可以为其中一个child设置layout_weight="2"然后为另一个child

设置layout_weight="8"

像这样

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10">


<imageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="8"
    />

<LinearLayoout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2" />