2011-02-07 22 views
1

我想在金字塔將舉辦7周的ToggleButtons,像這樣:Android Togglebuttons按金字塔組織?

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

其中B代表togglebuton和 - 代表一個空的空間。我也用整個金字塔來填補它的父母的寬度。我怎樣才能做到這一點?任何幫助表示讚賞。

回答

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" 
/> 

+0

謝謝!作品lika魅力....幾乎....但fth_left和fth_right不適合父母的寬度。 :(我如何可以設置所有按鈕具有相同的寬度,並填寫家長的寬度? – 2011-02-07 19:38:39