Android Togglebuttons被组织成金字塔?

时间:2011-02-07 14:40:21

标签: android layout togglebutton

我希望在金字塔中组织7个togglebuttons,如下所示:

---b---
--b-b--
-b---b- 
b-----b

其中b表示togglebuton, - 表示空白空间。我也用整个金字塔填充它的父母的宽度。我怎样才能做到这一点?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

使用RelativeLayout。

使顶部按钮具有layout_centerHorizo​​ntal =“true”,并将设置在顶部中间。 对于下一行,使用两个按钮layout_below =“@ id / id_of_your_top_button”,这样它们都会在顶部按钮下方对齐,然后,两者分别使用layout_toLeftOf =“@ id / id_of_your_top_button”和toRight,所以它们将是位于顶部按钮的左侧和右侧。只需重复第3和第4行。

示例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ToggleButton
    android:id="@+id/top"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
/>

<ToggleButton
    android:id="@+id/second_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/top"
    android:layout_toLeftOf="@id/top"
/>
<ToggleButton
    android:id="@+id/second_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/top"
    android:layout_toRightOf="@id/top"
/>

<ToggleButton
    android:id="@+id/third_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/second_left"
    android:layout_toLeftOf="@id/second_left"
/>
<ToggleButton
    android:id="@+id/third_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/second_right"
    android:layout_toRightOf="@id/second_right"
/>

<ToggleButton
    android:id="@+id/fth_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/third_left"
    android:layout_toLeftOf="@id/third_left"
/>
<ToggleButton
    android:id="@+id/fth_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/third_right"
    android:layout_toRightOf="@id/third_right"
/>

相关问题