android 中的 LinearLayout 中的 WeightSum 未按预期工作
WeightSum not working as expected in a LinearLayout in android
好吧,我有一个带有两个 children 的 LinearLayout,一个 EditText 和一个 Button,它们都有一个 weightSum 的 3
。我想要的只是一条带有 2
layout_weight EditText 和 1
layout_weight 按钮的水平线。这是我的代码:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="3">
<EditText
android:id="@+id/edit1"
android:inputType="textAutoComplete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DBLocations"
android:layout_weight="1"/>
</LinearLayout>
但是权重不起作用,结果如下:
screenshot of the wrong result
我必须做什么才能在 EditText 中使用 2
layout_weight 并在按钮中使用 1
?
您应该在 XML 代码中设置 android:orientation="horizontal"
。
并在 Button
和 EditText
中添加 android:layout_width="0dp"
。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal
android:weightSum="3">
<EditText
android:id="@+id/edit1"
android:inputType="textAutoComplete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/DBLocations"
android:layout_weight="1"/>
</LinearLayout>
将 'layout_width' 设置为 Button
的“0dp”并将 LinearLayout
的方向设置为 horizontal
,如图所示
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal
android:weightSum="3">
<EditText android:id="@+id/edit1"
android:inputType="textAutoComplete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<Button android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/DBLocations"
android:layout_weight="1"/>
</LinearLayout>
好吧,我有一个带有两个 children 的 LinearLayout,一个 EditText 和一个 Button,它们都有一个 weightSum 的 3
。我想要的只是一条带有 2
layout_weight EditText 和 1
layout_weight 按钮的水平线。这是我的代码:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="3">
<EditText
android:id="@+id/edit1"
android:inputType="textAutoComplete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DBLocations"
android:layout_weight="1"/>
</LinearLayout>
但是权重不起作用,结果如下:
screenshot of the wrong result
我必须做什么才能在 EditText 中使用 2
layout_weight 并在按钮中使用 1
?
您应该在 XML 代码中设置 android:orientation="horizontal"
。
并在 Button
和 EditText
中添加 android:layout_width="0dp"
。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal
android:weightSum="3">
<EditText
android:id="@+id/edit1"
android:inputType="textAutoComplete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/DBLocations"
android:layout_weight="1"/>
</LinearLayout>
将 'layout_width' 设置为 Button
的“0dp”并将 LinearLayout
的方向设置为 horizontal
,如图所示
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal
android:weightSum="3">
<EditText android:id="@+id/edit1"
android:inputType="textAutoComplete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<Button android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/DBLocations"
android:layout_weight="1"/>
</LinearLayout>