0

我正在关注教程,它说

所有视图的默认权重为 0,因此如果您只为一个视图指定任何大于 0 的权重值,那么在所有视图都获得所需空间后,该视图将填充剩余的空间。因此,要使用 EditText 元素填充布局中的剩余空间,请将其权重设为 1,并使按钮保持无权重。

你通过写作来实现这一点

<EditText
        android:layout_weight="1"
        ... />

我已经做到了。正如教程所说,我也将其设置layout_width为。0dp

现在,在 Android Studio 中,Preview 窗格中的一切看起来都很好。但是当我调试我的应用程序时,EditText控件没有出现。但是按钮出现在屏幕下方和最左侧,就好像EditText控件实际上已经填满了屏幕的整个宽度并将其向下推(除了EditText没有出现,所以我无法确认这一点)。

我已经删除了 weight 属性并在其上设置了固定宽度,并且可以显示,但显然,这是不可取的。

为什么不会EditText控件不显示?

这是我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" android:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:hint="@string/edit_message" />

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send" />


</LinearLayout>

如下所示,没有 EditText 控件。

在此处输入图像描述

4

2 回答 2

1

试试这个代码:你的 editText 将覆盖 appx。70% 并且您的按钮将覆盖剩余的总宽度...实际上,如果您使用 0dp 宽度方法,则必须为所有视图设置它 afaik...。

<EditText android:id="@+id/edit_message"
              android:layout_weight="3"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:hint="@string/edit_message" />

    <Button android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/button_send" />
于 2013-09-20T05:35:37.773 回答
1

罪魁祸首是你editText

android:layout_width="0dp"

替换为:

android:layout_width="107dip" 
于 2013-09-20T05:39:08.893 回答