2017-02-10 1 views
0

J'essaye de créer un GridLayout statique avec deux rangées et deux colonnes. Dans la rangée du bas, un bouton devrait être centré sur les deux colonnes. Au lieu de cela, le bouton semble être centrée uniquement dans la colonne de droite: (. J'étirées la hauteur du bouton pour l'asymétrie serait plus évidente)Pourquoi ne puis-je pas centrer un bouton sur deux colonnes d'un GridLayout Android?

enter image description here

J'attendais ces attributs dans l'élément Button pour la centrer sur les deux colonnes:

 android:layout_column="0" 
     android:layout_columnSpan="2" 
     android:layout_gravity="center_horizontal" 

Voici ma mise en page complète:

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnCount="2" 
    android:rowCount="2"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_row="0" 
     android:layout_column="0" 
     android:text="Title: " /> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_row="0" 
     android:layout_column="1"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:text="@string/search" 
     android:layout_row="1" 
     android:layout_column="0" 
     android:layout_columnSpan="2" 
     android:layout_gravity="center_horizontal"/> 
</GridLayout> 

Comment puis-je centrer un élément sur plusieurs colonnes?

Répondre

1

Réponse précédente semble bien pour moi, mais bien vous pouvez placer le bouton dans une disposition linéaire, quelque chose comme:

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnCount="2" 
    android:rowCount="2"> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="0" 
     android:layout_row="0" 
     android:text="Title: " /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:layout_row="0" 
     android:text="hello world" /> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_column="0" 
     android:layout_row="1" 
     android:layout_columnSpan="2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <Button android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="test buttontest buttontest button" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</GridLayout> 

Cela devrait faire ce que vous visez à :

result

+0

Cela a un sens total, et cela fonctionne. Merci beaucoup. –

0

enter image description here Essayez cette

<?xml version="1.0" encoding="utf-8"?> 

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:columnCount="2" 
android:rowCount="2"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_column="0" 
    android:layout_row="0" 
    android:text="Title: " /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_column="1" 
    android:layout_row="0" 
    android:text="hello world" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_column="0" 
    android:layout_columnSpan="2" 
    android:layout_gravity="center_vertical|center_horizontal" 
    android:layout_row="1" 
    android:text="search" /></GridLayout> 
+0

Merci, mais C'est aussi décentré, au moins sur mon système. C'est moins évident parce que le bouton n'est pas aussi grand. Je peux voir que c'est décentré quand je remplace "Title:" par une chaîne plus longue. –