Ellipsize不能正常工作了多行的TextView与任意最大高度 [英] Ellipsize not working properly for a multiline TextView with an arbitrary maximum height

查看:173
本文介绍了Ellipsize不能正常工作了多行的TextView与任意最大高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的TextView 与未知的最大高度,这取决于设备的DPI /屏幕分辨率。所以,举例来说,上和MDPI装置这个最大高度使得能够在一个时间只显示2行,可以提高到一个未定义数的值。

I have a TextView with an unknown maximum height, which is dependent on the device's DPI/screen resolution. So, for instance, on and MDPI device this maximum height makes it possible to show only 2 lines at a time, a value that can be increased up to an undefined number.

我的问题是有关与ellipsize功能。让我们假设目标设备允许要显示4行。如果我手动设置的行的最大数目,像这样...

My issue is related with the ellipsize functionality. Let's suppose that a certain device allows for 4 lines to be displayed. If I manually set the maximum number of lines, like this...

<TextView
    android:id="@+id/some_id"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:ellipsize="end" 
    android:maxLines="4"
    android:singleLine="false"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:text="This is some really really really really really long text"
    android:textSize="15sp" />

...一切正常确定。如果文本不适合正常,则省略号被添加在第四行的末尾,这样的:

...everything works OK. If the text doesn't fit properly, then the ellipsis are added at the end of the fourth line, like this:

This is some
really really
really really
really long...

但我宁愿的没有的设定的行数作为一个静态变量,因为我想preFER以包括DPI /屏幕分辨率的任意组合的支持。所以,如果我删除 MAXLINES 省略号不再正确显示在四个行,反而显示不完整的文字部分:

But I'd rather not set the number of lines as a static variable, as I would prefer to include support for any combination of DPI/screen resolution. So if I remove maxLines the ellipsis is no longer correctly shown at line four, showing instead an incomplete portion of text:

This is some
really really
really really
really long

如果我稍微增加的TextView 尺寸,我可以看到一个正在绘制的文本的其他部分隐藏在其他浏览。设置变量了maxHeight 似乎并没有擦出火花。

If I slightly increase the TextView size, I can see that the rest of the text is still being drawn "behind" the other Views. Setting the variable maxHeight doesn't seem to work either.

我仿佛真的无法找到针对此问题的解决方案。有任何想法吗?如果有帮助,我唯一的工作与Android V4.0.3及以上(API等级15)。

I really can't seem to find a solution for this issue. Any ideas? If it helps, I'm working only with Android v4.0.3 and up (API level 15).

推荐答案

计算有多少行融入的TextView 的TextView#的getHeight() 的TextView#getLineHeight()。然后调用的TextView#setMaxLines()

Calculate how many lines fit into the TextView with TextView#getHeight() and TextView#getLineHeight(). Then call TextView#setMaxLines().

ViewTreeObserver observer = textView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int maxLines = (int) textView.getHeight()
                / textView.getLineHeight();
        textView.setMaxLines(maxLines);
        textView.getViewTreeObserver().removeGlobalOnLayoutListener(
                this);
    }
});

这篇关于Ellipsize不能正常工作了多行的TextView与任意最大高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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