2011-02-07 6 views
1

私はこのように、7つのトグルボタンがピラミッドで編成することにしたい。Android Togglebuttonsはピラミッドとして編成されていますか?

bはtogglebutonを表し、
---b--- 
--b-b-- 
-b---b- 
b-----b 

- 空のスペースを表します。私はまた、ピラミッド全体を親の幅を埋めるようにうろついています。どうすればこれを達成できますか?どんな助けもありがとうございます。

答えて

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

ありがとうございました!あなたの魅力は....ほとんど変わりませんが、fth_leftとfth_rightは親の幅に収まりません。どのようにすべてのボタンを同じ幅に設定し、親の幅を塗りつぶすことができますか? –

+0

私が与えることができる唯一の簡単な提案は、アクティビティのToggleButtonの幅を計算し、ピラミッドをプログラマティックに配置し、 ToggleButtons内のすべてのwrap_contentはあなたが持っている幅の値になります。 –

関連する問題