Android安排按钮连续

时间:2014-02-21 21:50:53

标签: android android-layout

如何在这张照片中连续安装一些按钮 实际上我需要添加或删除按钮之间的空格;按钮必须稳定。 我总是左边有一个按钮,右边有一个按钮,中间有一个按钮,中间有两个按钮。如果屏幕尺寸发生变化,按钮之间的间隙将会改变。

提前感谢

此图显示了它如何在小屏幕中工作

enter image description here

图2展示了它在大屏幕中的表现。

enter image description here

按钮“1”始终为左;
按钮“5”始终为右;
按钮“3”始终为中心;

问题:如何让按钮“2”和“4”在其他按钮之间保持相同的空格?

1 个答案:

答案 0 :(得分:5)

这里我显示了4个按钮的配置。我们的想法是将按钮包装在水平LinearLayout中,并在layout_weight 1的按钮之间添加视图,以便在线性布局改变大小时缩小和扩展。

<LinearLayout
    android:id="@+id/lh"
    android:layout_width="208dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="15dp"
        android:text="B" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1" >
    </View>

    <Button
        android:id="@+id/Button01"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="B" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1" >
    </View>

    <Button
        android:id="@+id/Button02"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="B" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="1" >
    </View>

    <Button
        android:id="@+id/Button03"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="B" />
</LinearLayout>

或者,等效地,将Space替换为View以传达此特定视图的实际功能,但仅限于使用级别至少为14 的API。 Space是“一个轻量级的View子类,可用于在通用布局中创建组件之间的间隙”。