简体   繁体   中英

Android TextView with wrap_content clipping text lines

I'm creating a fragment in Android that has a long string added dynamically. The number of lines is variable. I'm using a LinearLayout to contain the text underneath an image. I would like the text to keep the width of the image and expand vertically underneath. When I use wrap_content on the layout_height for the TextView, it clips the text after the second line. I can set the height or the lines attributes and the parent LinearLayout expands correctly, how can I make this have a dynamic height using xml attributes?

Here is the layout code:

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

Here is the result:

在此处输入图片说明

And here is how I'd like it to look:

在此处输入图片说明

This can be fixed by giving your outer linear layout a specific width. Sorry, I've not seen a good explanation as to why this is the case, but it did work after I tested it.

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

In addition to setting a static width as Stephen proposed, if the text changes dynamically you also need to reset the height in a 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() );
            }
        } );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM