安卓,如何让两个按钮相邻,宽度相同

时间:2022-02-25 01:23:23

Im trying to place two buttons on the xml next to each other and I want them both to be the same size(half from the width of the screen) - this is my xml:

我试着把两个按钮放在xml上,放在一起,我希望它们都是相同的大小(屏幕宽度的一半)——这是我的xml:

<LinearLayout
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:id="@+id/linearLayout2"
        android:layout_height="fill_parent"
        android:gravity="bottom"
        android:weightSum="2">
    <Button
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bAddDelete"
        />

    <Button
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bMainMenu"
         />
</LinearLayout>

Everything works well, but when im changing the text in java, the width is changing, how can I make them the same size?

一切都运行得很好,但是当我在java中修改文本时,宽度在变化,我如何使它们保持相同的大小?

Thanks

谢谢

1 个解决方案

#1


4  

When you set the weight of the view then you shouldn't use any values for the width / height. Set the width of the buttons to 0dp. You also don't need android:weightSum="2"

设置视图的权重时,不应该对宽度/高度使用任何值。将按钮的宽度设置为0dp。你也不需要android:weightSum="2"

<LinearLayout
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:id="@+id/linearLayout2"
    android:layout_height="fill_parent"
    android:gravity="bottom">
<Button
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/bAddDelete"
    />

<Button
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/bMainMenu"
     />
</LinearLayout>

#1


4  

When you set the weight of the view then you shouldn't use any values for the width / height. Set the width of the buttons to 0dp. You also don't need android:weightSum="2"

设置视图的权重时,不应该对宽度/高度使用任何值。将按钮的宽度设置为0dp。你也不需要android:weightSum="2"

<LinearLayout
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:id="@+id/linearLayout2"
    android:layout_height="fill_parent"
    android:gravity="bottom">
<Button
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/bAddDelete"
    />

<Button
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/bMainMenu"
     />
</LinearLayout>