在 TextView 上设置 Ellipsize 会减少显示的行数(而不是仅最后一个椭圆化) [英] Setting Ellipsize on TextView reduces lines shown by one (instead of only ellipsizing last)

查看:15
本文介绍了在 TextView 上设置 Ellipsize 会减少显示的行数(而不是仅最后一个椭圆化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 TextViewsingleLine="true"ellipsize="end"(我的顶级 TextView)一起使用时,效果很好

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

但在另一个 TextView 有超过 1 行(在我的情况下,我的底部 TextView 中有 3 行),lines="3";和 maxLines=3";而 ellipsize=end", 不能正常工作.

but in another TextView having 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.

当我不把 ellipsize=end" 放在 tvDesc 中时,它显示 3 行,没关系.这是代码:XML:

When I DON'T put ellipsize="end" in tvDesc, it shows 3 line, which is OK. Here is code : 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>

上面的代码显示 TextView 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"
        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>

仅显示 2 行和 Ellipsize 的 TextView tvDesc

这不是正确的去zired UI,如Followin:

which is not properly dezired UI, as Followin:

我想在 TextView tvDesc 中用 ellipsize 显示 3 行

任何人都可以帮助我吗?

推荐答案

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

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!

哦,记得删除 XML 中的 android:ellipsize 标记,因为您将使用底部代码在 3 行末尾自动省略大小.

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

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);
        }
    }
});

这篇关于在 TextView 上设置 Ellipsize 会减少显示的行数(而不是仅最后一个椭圆化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆