Android:完全对齐两个按钮

时间:2014-03-03 10:27:44

标签: android-layout

我有一个线性布局,填充父级的布局参数和换行内容。方向是水平的。

在线性布局中,我有两个按钮。它们在左侧彼此相邻。

我希望这两个按钮“完全合理”。换句话说,我希望每个按钮占据线性布局宽度的一半。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

使用权重:

<LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">

 <Button
    android:text="Button1"
    android:id="@+id/button1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

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

答案 1 :(得分:-1)

为每个孩子的父级设定一个权重总和

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:weightSum="2">

     <Button
        android:text="Button1"
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

     <Button
        android:text="Button2"
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    </LinearLayout>
相关问题