1

Ich versuche, zwei TextViews nebeneinander und zentriert zu bekommen - damit sie wachsen können, wenn die Daten wachsen. Wenn der Text auch groß ist, mache ich es aus.Wie kann ich ein Android TextView mit viel Text anhalten, indem ich einen Text rechts ausquetsche?

Das Problem, das ich habe, ist, dass, wenn eine der Boxen viel Text enthält, es den anderen ausquetscht. Was ich möchte, ist, dass der Kleine voll dargestellt wird und der Große mehr gequetscht wird.

Derzeit, wenn ich den folgenden Text in den beiden Boxen

  • Eine große lange Liste von Wörtern, die die anderen Textview schiebt
  • Foobar

beide gequetscht zu werden.

In diesem Fall würde ich „Foobar“ mögen, vollständig angezeigt werden, wobei der längere Textview etwas Platz Aufgeben --- zB

...the other TextView out Foobar

Wenn ich jetzt bekommen, ist

...that pushes the other TextView out ...ar

Wenn beide Textfelder von ähnlicher Größe sind, möchte ich, dass sie beide einen fairen Anteil erhalten.

Mein Versuch ist unten.

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal"> 
    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:maxLines="1" 
     android:ellipsize="start" 
     android:textSize="20dp" 
     android:text="A big long list of words that pushes the other TextView out" /> 
    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:maxLines="1" 
     android:ellipsize="start" 
     android:textSize="20dp" 
     android:text="Foobar" /> 
</LinearLayout> 

Ich habe auch versucht, einen relativen Ausgang (siehe unten) --- mit einem minWidth --- aber das ist nur die am weitesten rechts stehende Textview aus der übergeordneten Ansicht vollständig zusammenzugedrückt!

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal"> 
    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:maxLines="1" 
     android:ellipsize="start" 
     android:textSize="20dp" 
     android:text="A big long list of words that pushes the other TextView out" /> 
    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/textView1" 
     android:minWidth="100dp" 
     android:maxLines="1" 
     android:ellipsize="start" 
     android:textSize="20dp" 
     android:text="Foobar" /> 
</RelativeLayout> 

Wer weiß, wie das funktioniert?

+0

Wird es immer der TextView1 sein, der den längeren Text hat? –

+0

Wenn Sie ihnen nur gleichen Anteil geben möchten, dann geben Sie das lineare Layout GewichtSumme = 2 und geben Sie die beiden Textansichten 0dp width und layout_weight = "1" – Anmol

+0

Manchmal TextView1 wird der längere sein. – dommer

Antwort

0

Werfen Sie einen Blick auf Googles FlexboxLayout. Ich denke, dass es die Fähigkeit haben kann, Ihre TextView s fließen zu lassen. Sie können einige Dokumentation here und ein Tutorial here finden.

FlexboxLayout ist ein Bibliotheksprojekt, das die ähnlichen Funktionen des CSS Flexible Box Layout-Moduls auf Android bringt.

Update: Ich bin nicht sicher, dass FlexboxLayout die Lösung. Hier ist eine weitere, die TableLayout und TableRow verwendet.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:shrinkColumns="0"> 

    <!-- TableLayout is needed to specify shrinkColumns. Ignore "Useless parent" error. --> 
    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     tools:ignore="UselessParent"> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:ellipsize="start" 
      android:maxLines="1" 
      android:text="A big long list of words that pushes the other outwords that pushes the other TextView out" 
      android:textSize="20sp" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginEnd="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_weight="1" 
      android:maxLines="1" 
      android:text="Foobar" 
      android:textSize="20sp" /> 

    </TableRow> 
</TableLayout> 
Verwandte Themen