为什么我的视图不显示宽度(layout_weight 不工作)

Why isn't my my view displaying a width (layout_weight not working)

我正在尝试创建一个包含 2 列的表单部分。这是一个非常直接的想法,但由于某种原因,我的布局无法正确呈现。我试图在左侧排列一个标签,在右侧排列一个 editText。我知道这个网站上还有其他类似的问题,但我无法弄清楚我的代码中缺少什么。看起来应该很简单,但我什么也没得到。下面是我的代码和正在发生的事情的屏幕截图。

布局权重采用整数,而不是 dp。您需要将 weightSum 应用于父级,在本例中为 LinearLayout。例如:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:weightSum="2">

    <Button
        android:id="@+id/avoidButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Left"/>

    <Button
        android:id="@+id/followButton"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Right"/>

</LinearLayout>

您应该使用 layout_weight="1",而不是 "1dp"。