2010-01-22 4 views
11

J'ai un LinearLayout simple avec un TextView et un ImageView. Je veux que le texte dans le TextView soit aligné à droite, mais le résultat montre que le texte a été aligné à gauche. Y a-t-il quelque chose qui ne va pas avec ma mise en page xml? Merci.android: layout_gravity ne fonctionne pas comme prévu

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:background="#E8E3E4"> 
    <LinearLayout android:orientation="horizontal" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     > 
     <TextView android:layout_width="260dp" android:layout_height="wrap_content" 
      android:text="More Comments" android:textStyle="bold" android:background="#ff0000" 
      android:textColor="#121222" android:layout_gravity="right" /> 
     <ImageView android:layout_width="50dp" 
      android:layout_height="wrap_content" android:src="@drawable/arrow_right" 
      android:layout_gravity="center" android:scaleType="centerInside" /> 
    </LinearLayout> 
</LinearLayout> 

Merci, Michael. Mais la solution de contournement que vous avez mentionnée ne fonctionne pas non plus.

J'ai essayé d'utiliser RelativeLayout, mais il m'a donné encore comme résultat: Plus Commentaires (Icône)

Ce que j'attendu est Plus Commentaires (Icône)

Le xml RelativeLayout est au-dessous. Y a-t-il quelque chose qui ne va pas?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <TextView android:id="@+id/label" android:layout_width="260dp" 
     android:layout_height="wrap_content" android:text="More Comments" 
     android:textStyle="bold" android:background="#ff0000" 
     android:textColor="#121222" android:layout_gravity="right" /> 
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content" 
     android:src="@drawable/arrow_right" android:layout_gravity="center" 
     android:scaleType="centerInside" android:layout_toRightOf="@id/label" /> 
</RelativeLayout> 

Après la solution de contournement dans le commentaire de Michael, j'ai le code XML ci-dessous. Je pensais que la sortie soit:

 RightLeft 

mais la sortie réelle est:

Right    Left 

Donc, la solution de contournement ne fonctionne pas pour moi. Des pensées?

<LinearLayout android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="right" 
        android:layout_weight="1.0"      
        android:text="RIGHT"/> 
      <TextView 
        android:layout_width="wrap_content" 

        android:layout_height="wrap_content" 
        android:layout_gravity="left" 
        android:text="LEFT"/> 
    </LinearLayout> 

+0

Pouvez-vous inclure votre xml? – jball

+0

Il l'a fait, il ne l'a pas formaté. –

+0

(aussi: édité pour qu'il soit visible, et visible * comme code * ...) –

Répondre

7

J'ai trouvé ce qui ne va pas avec mon xml. J'aurais dû utiliser la gravité au lieu de layout_gravity pour aligner à droite le texte dans TextView. Le code XML suivant fonctionne comme prévu.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <TextView android:id="@+id/label" android:layout_width="260dp" 
     android:layout_height="wrap_content" android:text="More Comments" 
     android:textStyle="bold" android:background="#ff0000" 
     android:textColor="#121222" android:gravity="right" /> 
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content" 
     android:src="@drawable/arrow_right" android:layout_gravity="center" 
     android:scaleType="centerInside" android:layout_toRightOf="@id/label" /> 
</RelativeLayout> 
+0

Ensuite, vous devez marquer cette réponse comme «acceptée» (cliquez sur la coche à gauche de la question). –

1

Je l'ai fait quelques projets avec Android, mais Android: layout_gravity spécifie l'alignement pour la vue au sein de la société mère, androïde: gravité spécifie l'alignement de la Vues contenues dans la vue dont vous spécifiez la gravité. Assurez-vous que vous avez le bon spécifié. La documentation ne fait pas la distinction entre ces deux.

Il serait plus facile de vous aider si vous avez publié un exemple de code XML.

EDIT:

Je vois que vous avez posté le XML maintenant. Il semble que vous ayez le bon ensemble d'attributs, mais si vous alignez le texte sur la droite, pourquoi le mettrez-vous en premier dans la disposition linéaire? Je suggère de le changer pour aller de gauche à droite.

33

Les profilés linéaires à orientation horizontale ne supportent pas la gravité droite/gauche. Vous pouvez trouver une solution de contournement here.

+0

+1, j'avais passé du temps à essayer de trouver cette information avant que j'ai vu cette réponse. –

0

Voici un code que j'ai écrit pour mon projet. Il accomplit ce que vous cherchez. Je voudrais juste changer les types de contrôle et tels et vous devriez être en marche.

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="50dip" 
    android:background="#ffdddddd"> 
    <TextView android:layout_width="260dp" 
     android:layout_height="wrap_content" 
     android:text="More Comments" 
     android:textStyle="bold" 
     android:background="#ff0000" 
     android:textColor="#121222" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentTop="true" /> 
    <ImageView android:layout_width="50dp" 
     android:layout_height="wrap_content" 
     android:src="@drawable/arrow_right" 
     android:scaleType="centerInside" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" /> 
</RelativeLayout> 
+0

Salut Jared, j'ai essayé votre code en remplaçant ToogleButton par TextView. Cela n'a pas fonctionné: le texte dans TextView n'était pas aligné à droite. – user256239

+0

Cela devrait fonctionner il suffit de le copier. – Jared

Questions connexes