Utilisez la relation RelativeLayout.
Faites en sorte que le bouton du haut ait layout_centerHorizontal = "true" et qu'il soit défini en haut au centre. Pour la ligne suivante, utilisez les deux boutons layout_below = "@ id/id_of_your_top_button" afin qu'ils soient tous les deux alignés sous votre bouton supérieur, puis, pour les deux, utilisez layout_toLeftOf = "@ id/id_of_your_top_button" et toRight respectivement. être positionné à gauche et à droite de votre bouton du haut. Répétez juste pour les 3ème et 4ème rangs.
Exemple:
<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"
/>
Merci! Fonctionne lika un charme .... presque .... mais les fth_left et fth_right ne tiennent pas dans la largeur du parent. :(Comment puis-je définir tous les boutons pour avoir la même largeur ET remplir la largeur du parent? –
La seule suggestion rapide que je peux vous donner, est de calculer la largeur d'un ToggleButton dans l'activité et la disposition de la pyramide programmatique, en remplaçant tout wrap_content dans ToggleButtons à n'importe quelle valeur de largeur que vous avez. –