2017-01-17 2 views
1

Ich füge TextView in eine LinearLayout, aber diese LinearLayout erweitert, wenn ich es mit Text (TextView) füllen. Es ist keine Option, es zu RelativeLayout oder etwas in der Art zu ändern, weil ich einem Führer von einem Buch folge. Das ist mein XML-Code:LinearLayout erweitert beim Hinzufügen von TextView

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="40" 
     android:background="@android:color/holo_orange_light"> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent"> 

       <TextView 
        android:text="" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:id="@+id/textView" /> 
      </LinearLayout> 
     </ScrollView> 
    </LinearLayout> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="60"> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:srcCompat="@mipmap/ic_launcher" 
      android:id="@+id/imageView" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</LinearLayout> 
+0

Ofcourse weil Eltern mit wrap_content passt sich an die Child-Breite an (die größte unter seinem Kind). –

+0

Was ist das normale Verhalten einer Ansichtsgruppe (wie Linearlayout)? Aber was ist deine Frage? Willst du ein anderes Verhalten? – Dibzmania

Antwort

0

Ich bin nicht sicher genau das, was Sie es tun wollen, sondern versucht, die layout_width des Textview zu ändern und das Linearlayouts ist es in zu „match_parent“.

0

Wenn Sie Text TextView hinzufügen, wird die Breite von TextView erweitert, wenn der Text größer wird und die Breite auf "wrap_content" gesetzt wird, so dass die Breite des Textes ist. Als Ergebnis erhält LinearLayout, das die Elternansicht dieser TextView ist, eine größere Breite, da es auf "wrap_content" gesetzt ist. Und dadurch wird ScrollView größer und dadurch wird das äußere LinearLayout größer.

Lösung zu diesem wäre LinearLayout Breite auf "match_parent" oder eine beliebige Breite ("50dp" zum Beispiel). Im Falle der Einstellung "match_parent" würde die LinearLayout-Breite die Breite des Bildschirms sein, aber Sie müssen "match_parent" sowohl auf LinearLayouts als auch auf ScrollView setzen.

Ich hoffe, dass Ihre Frage beantwortet

0

Das Problem in Ihrem Code ist in XML-Datei, in der Sie die Höhe Textview angeben:

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="40" 
    android:background="@android:color/holo_orange_light"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent"> 
     <!--Change your textView as below--> 
      <TextView 
       android:text="" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView" /> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 
<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="60"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:srcCompat="@mipmap/ic_launcher" 
     android:id="@+id/imageView" 
     android:layout_weight="1" /> 
</LinearLayout> 

Verwandte Themen