2017-01-17 7 views
1

Я добавляю TextView в LinearLayout, но этот LinearLayout расширяется, когда я заполняю его текстом (TextView). Это не вариант, чтобы изменить его на RelativeLayout или что-то в этом роде, потому что я следую руководству из книги. Это мой XML-код:LinearLayout расширяется при добавлении 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 потому, что родитель с wrap_content будет регулировать его ширину для ребенка (наибольшую из ее дочерних элементов). –

+0

Что вы видите, это нормальное поведение группы вида (например, Linearlayout). Но каков ваш вопрос? Вам нужно другое поведение? – Dibzmania

ответ

0

Я не уверен, что именно вы хотите, чтобы это сделать, но попробуйте изменить layout_width в TextView и LinearLayout это, чтобы «match_parent».

0

Когда вы добавляете текст в ширину TextView этого TextView, расширяется по мере увеличения текста, а ширина устанавливается равной «wrap_content», поэтому его ширина - это ширина текста, который он удерживает. В результате LinearLayout, который является родительским представлением этого TextView, получает большую ширину, так как он установлен на «wrap_content». И в результате этого ScrollView становится больше, и из-за этого внешний LinearLayout становится больше.

Решение этой задачи заключалось бы в том, чтобы установить ширину LinearLayout равной «match_parent» или некоторую произвольную ширину (например, 50dp). В случае установки «match_parent» ширина LinearLayout будет шириной экрана, но вам нужно будет установить «match_parent» как для LinearLayouts, так и для ScrollView.

Я надеюсь, что ответы на ваш вопрос

0

Проблема в коде в XML-файле, где вы определяете, высоту TextView:

<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>