Android 在 TextView 上设置 Ellipsize 将显示的行减少一个(而不是只在最后一个省略号)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11210553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:20:39  来源:igfitidea点击:

Setting Ellipsize on TextView reduces lines shown by one (instead of only ellipsizing last)

androidtextviewellipsis

提问by Chintan Raghwani

when I am using TextViewwith singleLine="true"and ellipsize="end"(my top TextView), it works well

当我使用TextViewsingleLine="true"ellipsize="end"(我的前的TextView), 效果很好

but in anotherTextViewhaving more then 1 lines (in my case 3 lines in my bottom TextView), lines="3" and maxLines="3" and ellipsize="end",doesn't work properly.

但是在另一个TextView有超过 1 行的情况下(在我的情况下,我的底部有 3 行TextViewlines="3" and maxLines="3" and ellipsize="end",无法正常工作。

When I DON'T put ellipsize="end"in tvDesc, it shows 3 line, which is OK. Here is code : XML :

当我不放入ellipsize="end"tvDesc 时,它显示 3 行,这是可以的。这是代码: XML :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imgv"
        android:layout_width="65dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/img1"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imgv"
        android:background="@android:color/white"
        android:textColor="@android:color/black"
        android:text="Title testing line number and ellipsize at end"
        android:maxLines="1"
        android:singleLine="true"
        android:ellipsize="end"    <---  WORKS WELL
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/tvDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvTitle"
        android:layout_toRightOf="@+id/imgv"
        android:layout_alignBottom="@+id/imgv"
        android:layout_alignParentRight="true"
        android:textSize="14dp"
        android:lines="3"
        android:maxLines="3"
                       <----------  WITHOUT ellipsize

        android:text="I wanna this textview of having 3 lines and ellipsize at END and at this time I am Testing for it. This TextView shows 3 lines WITHOUT ellipsize property, but shows only 2 Lines when ELLIPSIZE property is setted"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Above code shows TextView tvDesc with 3 Lines and No-Ellipsize.Here is Image :

上面的代码显示了具有 3 行和无椭圆大小的 TextView tvDesc。这是图像:

enter image description here

在此处输入图片说明

But, I wanna ellipsize, so I uses followin code: XML :

但是,我想省略号,所以我使用以下代码:XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imgv"
        android:layout_width="65dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/img1"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imgv"
        android:background="@android:color/white"
        android:textColor="@android:color/black"
        android:text="Title testing line number and ellipsize at end"
        android:maxLines="1"
        android:singleLine="true"
        android:ellipsize="end"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/tvDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvTitle"
        android:layout_toRightOf="@+id/imgv"
        android:layout_alignBottom="@+id/imgv"
        android:layout_alignParentRight="true"
        android:textSize="14dp"
        android:lines="3"
        android:maxLines="3"
        android:ellipsize="end"    <------  WITH  ELLIPSIZE

        android:text="I wanna this textview of having 3 lines and ellipsize at END and at this time I am Testing for it. This TextView shows 3 lines WITHOUT ellipsize property, but shows only 2 Lines when ELLIPSIZE property is setted"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Which shows TextView tvDesc with only 2 lines and Ellipsize,

它显示只有 2 行和 Ellipsize 的 TextView tvDesc

which is not properly dezired UI, as Followin: enter image description here

这不是正确 dezired UI,如下: 在此处输入图片说明

I wanna 3 lines in TextView tvDesc with ellipsize

我想在 TextView tvDesc 中使用 3 行椭圆

CAN ANYONE HELP ME.?

谁能帮我。?

回答by Vishwa Patel

This is by far the simplest solution I've found and am currently using in deployment. Let me know if you need any other assistance!

这是迄今为止我发现的最简单的解决方案,目前正在部署中使用。如果您需要任何其他帮助,请告诉我!

Oh and remember to remove the android:ellipsizetag in your XML since you will be using the bottom code to automatically ellipsize at the end of 3 lines.

哦,记得删除android:ellipsizeXML中的标记,因为您将使用底部代码在 3 行末尾自动省略号。

TextView snippet;
snippet.setText("loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor ")
ViewTreeObserver vto = this.snippet.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        ViewTreeObserver obs = snippet.getViewTreeObserver();
        obs.removeGlobalOnLayoutListener(this);
        if (snippet.getLineCount() > 3) {
            int lineEndIndex = snippet.getLayout().getLineEnd(2);
            String text = snippet.getText().subSequence(0, lineEndIndex - 3) + "...";
            snippet.setText(text);
        }
    }
});

回答by irscomp

Just set android:maxLinesand android:ellipsize.

只需设置android:maxLinesandroid:ellipsize

<TextView
        android:id="@+id/tv_dua"
        android:ellipsize="end"
        android:maxLines="3"
        android:text="long text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

回答by Shivam Bhalla

Use the following to get a multiline textview with ellipsis on the last line :

使用以下命令获取最后一行带有省略号的多行文本视图:

android:maxLines="4"
android:ellipsize="end"
android:singleLine="false"

Replace 4 with the number of lines you want. Hope it helps !

将 4 替换为您想要的行数。希望能帮助到你 !

回答by McOzD

I tried it out with maxLines and ellipsize on Android 7 & 8.

我在 Android 7 和 8 上使用 maxLines 和 ellipsize 进行了尝试。

android:maxLines="3"
android:ellipsize="end"

The preview shows 2 lines and on the third line "...".

预览显示 2 行,第三行“...”。

But this seems to be a bug in the previewer.

但这似乎是预览器中的一个错误。

On the device it just works fine, 3 lines with text, at the end of the third line "..."

在设备上它工作正常,3 行文本,在第三行“...”的末尾

回答by flawyte

Just use ellipsizecombined with scrollHorizontally="true". Simple & clean.

只需将ellipsizescrollHorizontally="true". 简单干净。

It worked perfectly for me.

它非常适合我。

回答by Vinothkumar Arputharaj

Try it

尝试一下

tv.setSingleLine(false);
tv.setEllipsize(TextUtils.TruncateAt.END);
int n = 3; // the exact number of lines you want to display
tv.setLines(n);

refer Programmatically create TextView with ellipsis

参考以编程方式创建带有省略号的 TextView