2017-09-25 12 views
0

J'essaie de faire un chatview pour une application android, mais j'ai un problème avec la bulle de chat serrant le temps de message jusqu'à ce qu'il l'effondre complètement, puis il enveloppe à la rangée suivante. Ceci est le xml:Un TextView est en train d'en expulser un autre quand il se dilate au lieu d'envelopper

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/root" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginBottom="4dp" 
android:layout_marginTop="4dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="left|center" 
    android:orientation="horizontal" 
    android:weightSum="1"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.8" 
     android:gravity="left|center" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/content" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/chat_levi" 
      android:padding="10dp" 
      android:text="Insert text here" 
      android:textColor="@color/blueGray50" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/time" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="bottom|left" 
      android:text="18:30" 
      android:textSize="14sp" /> 

    </LinearLayout> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="0.2" /> 

</LinearLayout> 

Et une image pour vous montrer comment il ressemble: enter image description here

Et voici à quoi il ressemble quand il enveloppe: enter image description here

J'ai aussi une autre disposition pour l'autre utilisateur dans le chat, et pour lui je viens de changer tous les gauches de gravité aux droits, et a échangé les places de certains elemen ts, comme on peut le voir dans la dernière image (son code très similaire, donc je l'ai exclu, mais si quelqu'un le demande, je vais l'ajouter). Pour celui-là, j'obtiens l'effet désiré.

enter image description here

Y at-il une sorte de solution pour obtenir le premier à se comporter de la même manière que celle en miroir?

+1

vous pouvez ajouter les proprety « android: maxLines: '1 "".Cela permettra à votre TextView d'avoir seulement 1 ligne –

+0

Cela fait en sorte que le TextView pour l'heure n'emballe pas son texte à la ligne suivante avant qu'il ne soit évincé. –

+0

Pourquoi ne donnez-vous pas à l'heure une largeur fixe? – Journey

Répondre

0

Je suis venu avec une solution. Parce que le mirrored fonctionne parfaitement, j'ai fini par refléter le (RelativeLayout du "mirrored") avec android:scaleX="-1", et après j'ai utilisé android:scaleX="-1" sur les éléments TextView.

Voici le code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/root" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginBottom="4dp" 
android:layout_marginTop="4dp" 
android:scaleX="-1"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="right|center" 
    android:orientation="horizontal" 
    android:weightSum="1"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="0.2" /> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.8" 
     android:gravity="right|center" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/time" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="bottom|right" 
      android:scaleX="-1" 
      android:text="18:30" 
      android:textSize="14sp" /> 

     <TextView 
      android:id="@+id/content" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/chat_levi" 
      android:padding="10dp" 
      android:scaleX="-1" 
      android:text="Insert text here" 
      android:textColor="@color/blueGray50" 
      android:textSize="16sp" /> 

    </LinearLayout> 

</LinearLayout> 

Et le résultat final:

enter image description here

enter image description here

0

Essayez le code suivant:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="4dp" 
    android:layout_marginTop="4dp"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="left|center" 
     android:orientation="horizontal"> 

     <FrameLayout 
      android:layout_weight="0.8" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/chat_levi" 
       android:padding="10dp" 
       android:layout_width="wrap_content" 
       android:text="Insert texaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaat here" 
       android:textColor="@color/blueGray50" 

       android:textSize="16sp" /> 
     </FrameLayout> 


     <TextView 
      android:id="@+id/time" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:gravity="bottom|left" 
      android:text="18:30" 
      android:textSize="14sp" /> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0.2" /> 
    </LinearLayout> 
</RelativeLayout> 

màj: espacement fixe à la fin

+0

Cela ne fonctionne pas. Le texte est encore évincé, et j'ai besoin de l'espace à la fin. –

+0

s'il vous plaît vérifier le code mis à jour, j'ai été manqué avec la version du fichier édité.Cependant, c'est une solution de contournement et je suggère d'utiliser ConstraintLayout pour éviter les vues imbriquées. –

+0

Avec ce code, la vue spacer à la fin est évincée. –

0

Il y a probablement un million de façons de la peau ce chat, mais vous utilisez déjà des poids pour faire votre mise en page linéaire , vous pourriez faire quelque chose de très similaire pour votre textviews:

<TextView 
      android:id="@+id/content" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="4" 
      android:padding="10dp" 
      android:text="Insert ioadjfojdajidijfaseidfijoajsdifjasdijfijsadpfaosdjiofipasjdifapsjdfjasdjfasidjfasdjfoipjoijoij here" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/time" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="match_parent" 
      android:gravity="bottom|left" 
      android:text="18:30" 
      android:textSize="14sp" /> 

vous pouvez jouer avec les poids pour donner plus ou moins de place à votre horodatage.

EDIT ******

L'autre façon à la peau ce chat serait alors juste mettre maxwidth sur votre xml

<TextView 
      android:id="@+id/content" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:maxWidth="250dp" 
      android:text="Insert ijpoijiopjiopjiopjiopsdjfaisdjfipajsdifajsdifaisdfaidspjfipajdf here" 

      android:textSize="16sp" /> 

Vous devrez gérer pour différentes mises en page, bien que .

+0

Lorsque j'ajoute un poids au TextView avec la bulle, la bulle s'étend automatiquement pour prendre son poids. Je veux que la bulle (arrière-plan) enveloppe le texte. Je veux que l'un avec le temps ait une taille fixe et ne soit pas repoussé. –

+0

Je sais que c'est faisable avec maxwidth, mais je perds beaucoup de liberté là-bas. Le miroir fonctionne avec un code presque identique, et il est optimisé pour tout. –