2017-04-12 18 views
0

私はAndroidに動的に追加された長い文字列を持つフラグメントを作成しています。行数は可変です。私はLinearLayoutを使って画像の下にテキストを格納しています。私はテキストの下に画像の幅を維持し、垂直方向に拡大したいと思います。 TextViewのlayout_heightでwrap_contentを使用すると、2行目の後にテキストがクリップされます。 height属性またはlines属性を設定でき、親LinearLayoutが正しく展開されます。これをxml属性を使用して動的な高さにするにはどうすればよいですか?ここでwrap_contentのテキスト行をクリッピングするAndroid TextView

は、レイアウトコードです:ここで

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    ~ Copyright 2017, University of Colorado Boulder 
    --> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="vertical" 
    android:padding="24dp"> 

    <LinearLayout 
     android:id="@+id/image_and_favorite" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingBottom="8dp"> 

     <ImageView 
      android:id="@+id/image" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:onClick="launchSimulation" 
      android:paddingEnd="20dp" 
      android:scaleType="fitStart" 
      app:srcCompat="@drawable/logo" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/description" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. " /> 

</LinearLayout> 

は結果である:

enter image description here

enter image description here

そして、ここでは、私はそれを見てみたい方法です

答えて

1

これは、あなたの外衣を与えることによって修正することができます耳のレイアウトは特定の幅です。申し訳ありませんが、なぜこれが当てはまるのかという良い説明は見当たりませんでしたが、テストした後にはうまくいきました。スティーブンとして静的な幅を設定することに加え

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    ~ Copyright 2017, University of Colorado Boulder 
    --> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="150dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="vertical" 
    android:padding="24dp"> 

    <LinearLayout 
     android:id="@+id/image_and_favorite" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingBottom="8dp"> 

     <ImageView 
      android:id="@+id/image" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:onClick="launchSimulation" 
      android:paddingEnd="20dp" 
      android:scaleType="fitStart" 
      app:srcCompat="@drawable/logo" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/description" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. " /> 

</LinearLayout> 
+0

これは私にとっても役立ちました。ありがとう。 –

+0

これは実行時にテキストが変更されても機能しません。 –

0

テキストの変更は動的にあなたも実行可能なポストに高さをリセットする必要がある場合は、提案しました。

final TextView descriptionView = (TextView) view.findViewById(R.id.description); 
     descriptionView.setText(someLongString); 
     descriptionView.post(new Runnable() { 
      @Override 
      public void run() { 
       // ¯\_(ツ)_/¯ 
       descriptionView.setLines(descriptionView.getLineCount()); 
      } 
     }); 
関連する問題