2009-07-24 11 views
69

Je voudrais créer une boîte de dialogue pour afficher un titre vidéo et des balises. Sous le texte, j'aimerais ajouter les boutons View, Edit et Delete et rendre ces éléments de la même taille. Est-ce que quelqu'un sait comment modifier le fichier de mise en page. Xml afin de rendre les éléments à l'intérieur de LinearView même taille?Android: Comment faire tous les éléments à l'intérieur LinearLayout même taille?

Le fichier de configuration actuelle ressemble à ceci:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txtTitle" android:text="[Title]" > 
      </TextView> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/txtTags"    
       android:text="[Tags]" > 
      </TextView> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnPlay" 
      android:text="View"> 
     </Button> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnEdit" 
      android:text="Edit"> 
     </Button> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnDelete" 
      android:text="Delete"> 
     </Button> 

    </LinearLayout> 

</LinearLayout> 

Je vous serais reconnaissant si quelqu'un pouvait montrer la solution en modifiant le contenu du fichier collé.

Merci!

Répondre

148

Utilisez android:layout_width="0px" et android:layout_weight="1" sur les trois Button s. Cela signifie que les boutons ne devraient pas prendre plus de 0 pixels, mais les trois d'entre eux devraient séparer tout espace supplémentaire entre eux. Cela devrait vous donner l'effet visuel que vous souhaitez.

+4

Si vous avez _REALLY_ voulu vous pourriez alternativement utiliser un TableLayout avec android: stretchColumns = "0,1,2". –

+0

Génial ... donc cela signifie que si nous mettons la largeur à 0px, le poids écrase les paramètres? Pourquoi est-ce vrai? – noob

+0

Cela a fonctionné juste parfait. – Proverbio

28

Une autre façon est de faire android:layout_width="fill_parent" et android:layout_weight="1" cela fonctionnera aussi bien !!!

+13

Vous étiez vraiment excité à propos de cette réponse. :-) Aimez l'enthousiasme! –

+0

La solution de CommonsWare n'a pas fonctionné pour moi, mais cela l'a fait! :) J'ai utilisé "match_parent" depuis que j'ai entendu que vous devriez le préférer à "fill_parent", mais les deux fonctionnent. – codepleb

1

J'ai écrit un EqualSpaceLayout qui, je pense, résoudra votre problème. Ceci est similaire à LinearLayout, mais il ne déforme pas la taille des vues enfants. Vérifiez-le: http://www.dev-smart.com/archives/225

16

Utilisez LinearLayout avec votre weightSum et créez les éléments avec layout_weight. Voici un exemple ...

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="5"> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_share_white_36dp"/> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_search_white_36dp"/> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_event_note_white_36dp"/> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_brush_white_36dp"/> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ic_menu_white_36dp"/> 
</LinearLayout> 

Ainsi, la somme de poids de tous les éléments est 5. Voici la capture d'écran ...

enter image description here

Notez que, Google Material Design icônes sont utilisées. J'espère que c'est utile.

Questions connexes