2011-09-15 4 views
1

Dans mon LinearLayout, l'utilisation du poids en combinaison avec la gravité provoque l'inversion de l'effet du poids. La mise en page:LinearLayout avec gravité et poids

<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:layout_weight="0.2"> 
    <TextView android:id="@+id/feedfragmentTitle" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="8"/> 
    <TextView android:id="@+id/feedfragmentDate" android:layout_height="wrap_content" android:gravity="right" android:layout_width="fill_parent" android:layout_weight="2"/> 
</LinearLayout> 

Le LinearLayout donné contient un titre, qui devrait appromately prendre 80% de la mise en page et une date + heure avec 20%. La date et l'heure doivent avoir la gravité de droite. La mise en page que j'ai posté ne fonctionne malheureusement pas et si je joue avec les paramètres de poids, le résultat est inversé.

Existe-t-il un autre moyen d'obtenir les résultats sans permuter les poids?

Répondre

7

Lorsque vous utilisez la propriété layout_weight vous devez faire la hauteur appropriée ou largeur 0dip

essayer

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView android:id="@+id/feedfragmentTitle" 
     android:layout_height="wrap_content" android:layout_width="0dip" 
     android:layout_weight="8" android:text="sdfdfsfsd"/> 
    <TextView android:text="aass" android:id="@+id/feedfragmentDate" 
     android:layout_height="wrap_content" 
     android:gravity="right" android:layout_width="0dip" 
     android:layout_weight="2"/> 
</LinearLayout> 
+0

fonctionne très bien! Mais pourquoi ne fonctionne pas ma solution? Y at-il un problème ou est-ce juste un comportement d'Android/LinearLayout? J'ai utilisé dans un autre projet une construction similaire et cela a bien fonctionné. – kadir

+0

http://stackoverflow.com/questions/5986793/in-android-layouts-what-is-the-effect-meaning-of-layout-height-0dip – blessenm

0

Essayez comme ça

<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
<TextView android:id="@+id/feedfragmentTitle" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="left"/> 
<TextView android:id="@+id/feedfragmentDate" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_width="wrap_content" /> 
</LinearLayout> 
Questions connexes