带有wrap_content剪辑文本行的Android TextView

问题描述:

我在Android中创建了一个具有动态添加的长字符串的片段。行数是可变的。我使用LinearLayout来包含图像下方的文本。我希望文字保持图像的宽度并在下面垂直展开。当我在TextView的layout_height上使用wrap_content时,它在第二行之后剪切文本。我可以设置高度或线条属性,并且父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

这可以通过给你的外部林耳朵布局特定的宽度。对不起,我还没有看到一个很好的解释,为什么这是这种情况,但它测试后确实有效。

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

这对于在运行时更改的文本不起作用。 –

除了像Stephen提出的那样设置静态宽度,如果文本动态变化,您还需要重置可运行后的高度。

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