2011-09-20 6 views
0

J'ai un problème weired avec la mise en page de mon application. J'ai un listview, dans lequel j'ai inséré une propre mise en page pour chaque article. Cela fonctionne bien.problème linearlayout

Voici une partie de la mise en page XML inséré:

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
    > 
     <TextView 
      android:id="@+id/label" 
      android:paddingTop="2px" 
      android:paddingLeft="15px" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="16sp" /> 
     <TextView 
      android:id="@+id/price" 
      android:paddingTop="2px" 
      android:paddingLeft="15px" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="12sp" /> 
     <TextView 
      android:id="@+id/days" 
      android:paddingTop="2px" 
      android:paddingLeft="15px" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="12sp" /> 
     <TextView 
      android:id="@+id/test" 
      android:paddingTop="2px" 
      android:paddingLeft="15px" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="12sp" /> 
     <TextView 
      android:id="@+id/status" 
      android:paddingTop="2px" 
      android:paddingLeft="15px" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="12sp" /> 
    </LinearLayout> 

Dans mon activité, je fais ce qui suit:

tv = (TextView) row.findViewById(R.id.price); 
tv.setText(getString(R.string.wishlist_price) + ": " + "293.99€"); 

getString (R.string.wishlist_price) est défini dans string.xml comme "Prix"

Maintenant la chose étrange: Pour la "293.99 €" ci-dessus "la sortie sur le téléphone ressemble à ceci:

Price: 293.99€ 

Mais si je change l'argent, par exemple à 22,95 €, la sortie ressemble à ceci:

Price: 
22.95€ 

Alors il fait un saut de ligne après « Prix: » Je ne peux pas trouvé ce qui est à l'origine ce.

Je l'ai essayé avec « 2,99 € », il donne la sortie prévue:

Price: 2.99€ 

Est-ce que quelqu'un a une idée de ce qui est à l'origine du saut de ligne dans l'exemple « 22,95 € »?

EDIT: Il existe deux configurations linéaires autour de celle ci-dessus. Peut-être qu'ils provoquent ce comportement étrange:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
> 
    <LinearLayout 
     android:id="@+id/product_item" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
    > 
+0

Votre texte de la question présente quelques problèmes de formatage. Essayez de placer votre sortie dans un bloc de code afin que nous puissions voir ce que vous voulez dire. –

+0

Désolé. Maintenant, je l'ai édité – tobias

+0

Un autre commentaire avant de répondre. N'utilisez pas d'unités de pixels, utilisez toujours des pixels indépendants de la densité ('dp' ou' dip'). Votre interface évoluera beaucoup mieux avec les différentes résolutions sur les appareils Android. –

Répondre

0

Je l'ai résolu:

J'ai changé wrap_content en fill_parent:

<TextView 
       android:id="@+id/price" 
       android:paddingTop="2dp" 
       android:paddingLeft="15dp" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="12dp" /> 
+1

C'est bizarre que ce n'était pas envelopper votre contenu correctement. Il se peut qu'il ne soit pas réajusté lorsque vous avez ajouté ce texte. Si vous n'avez pas besoin de 'wrap_content' pour plusieurs choses sur une ligne, j'utiliserais toujours' fill_parent'. Si vous êtes dans le niveau API 8+, utilisez 'match_parent' à la place. Il remplace le 'fill_parent' déprécié. –

1

Cela peut ou peut ne pas fonctionner, mais en utilisant String.format(String, Object...) semble comme un bon ajustement pour ce que vous faites.

d'abord changer votre chaîne R.string.wishlist_price à

<string name="wishlist_price">Price: %1$s</string> 

puis l'appeler en utilisant ceci:

tv = (TextView) row.findViewById(R.id.price); 
tv.setText(String.format(getResources().getString(R.string.wishlist_price), price)); 

Jetez un oeil à ce pour plus d'informations: Using String Resources

+0

le prix est une valeur de chaîne. Qu'est-ce que je dois changer? Votre code ne fonctionne que si j'utilise une valeur entière pour "price" – tobias

+0

Vous changez le 'd' en un' s' dans la chaîne xml. Android utilise un Formatter pour formater la chaîne. La référence pour cela est ici: http://developer.android.com/reference/java/util/Formatter.html. –

+0

Votre code fonctionne.MAIS j'ai toujours le même problème: Le prix "22,95 €" se situe sous "Prix:". Avez-vous une autre idée – tobias