【问题标题】:Android TextView with wrap_content clipping text lines带有 wrap_content 剪切文本行的 Android TextView
【发布时间】:2017-04-12 01:14:50
【问题描述】:

我正在 Android 中创建一个动态添加长字符串的片段。行数是可变的。我正在使用 LinearLayout 来包含图像下方的文本。我希望文本保持图像的宽度并在下方垂直扩展。当我在 TextView 的 layout_height 上使用 wrap_content 时,它会在第二行之后剪辑文本。我可以设置 height 或 lines 属性,并且父 LinearLayout 可以正确展开,如何使用 xml 属性使其具有动态高度?

这是布局代码:

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

结果如下:

这是我想要的样子:

【问题讨论】:

    标签: android layout


    【解决方案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>
    

    【讨论】:

    • 这不适用于在运行时更改的文本。
    【解决方案2】:

    除了像 Stephen 建议的那样设置静态宽度之外,如果文本动态变化,您还需要在 post runnable 中重置高度。

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

    【讨论】:

      猜你喜欢
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多