WeightSum 在 android 的 LinearLayout 中没有按预期工作

-1 android android-layout-weight

好吧,我有一个带有两个孩子的 LinearLayout,一个 EditText 和一个 Button,它们的weightSum都是3. 我想要的只是一条带有2layout_weight EditText 和1layout_weight Button的水平线。这是我的代码:

<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>
Run Code Online (Sandbox Code Playgroud)

但是权重不起作用,结果如下: 错误结果的屏幕截图 为了2在 EditText 和1Button 中拥有layout_weight我必须做什么?

KeL*_*yue 5

您应该android:orientation="horizontal"在您的 XML 代码中进行设置。

并添加android:layout_width="0dp"您的ButtonEditText

<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>
Run Code Online (Sandbox Code Playgroud)