当LinearLayout的宽度为match_parent时,layout_weight = “1”需要使用单位。

3

我使用 android: layout_weight = "1" 来对齐元素,但我不明白为什么需要指定计量单位。

这是为什么呢?

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp">
        <Button
            android:text="+"
            android:layout_width="odp"
            android:layout_height="65dp"
            android:layout_weight="1"// requires a unit
            android:background="@android:color/black"
            android:textColor="@android:color/white"
            android:id="@+id/bttnPlus"
            android:layout_marginRight="20.0dp" />
        <Button
            android:text="-"
            android:layout_width="odp"
            android:layout_height="65dp"
            android:layout_weight="1"// requires a unit
            android:background="@android:color/black"
            android:textColor="@android:color/white"
            android:layout_marginRight="20.0dp"
            android:id="@+id/bttnMinuse" />
    </LinearLayout>

我可以确认只用1也能很好地运行。这是一个很好的问题,因为我也觉得很奇怪,只是从来没有问过。 - Ivan Ičin
2个回答

1
它不需要单位,这是一个Visual-Studio的问题。
在这个线程中:
它应该在Visual-Studio v16.9预览版中修复。

0

我认为问题不在于 layout_weight。你的 layout_width 有拼写错误。

android:layout_width="0dp"

替代

android:layout_width="odp"

我将 android:layout_width="odp" 更改为 android:layout_width="0dp",但错误仍然存在。 - bahmN
是的,问题不在于打错字。 - nevermore

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