我正在关注本教程,它说
所有视图的默认权重为 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 控件。