检查textview是否在android中是椭圆的 [英] Check if textview is ellipsized in android

查看:13
本文介绍了检查textview是否在android中是椭圆的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 TextView,宽度为 wrap content.在此 TextView 中,我设置了文本,但文本每次的长度都不相同.当文本很长时,我使用单行 true 和 ellipsize: end.但现在我有一个问题.我想设置其他布局的可见性,但这取决于我的文本长度.如果文本太长而不适合屏幕,我想将可见性设置为 true,但是当文本很短且不需要椭圆大小时,我想将可见性设置为 false.所以我需要检查我的 TextView 的状态.当它的椭圆大小时,我想 setVisible 为真,而不是 setVisible 为假.我怎么能做到这一点.这是我得到的:

I have TextView with width as wrap content. In this TextView I set text, but text is not of the same length every time. When text is very long I use single line true and ellipsize: end. But now I have a problem. I want to set Visibility of other layout but that depends on the length my text. If text is too long to fit in the screen I want to setVisible true, but when text is short and when I don't need ellipsize, I want to set visibility false. So I need to check status of my TextView. When its ellipsize I want to setVisible true, when its not setVisible false. How I can do that. This is what I got:

tvAle.post(new Runnable() {

        @Override
        public void run() {

            int lineCount    = tvAle.getLineCount();
            Paint paint =  new Paint();
            paint.setTextSize(tvAle.getTextSize());
            final float size = paint.measureText(tvAle.getText().toString());
            Log.v("a", ""+size+" "+tvAle.getWidth());
            if ((int)size > (tvAle.getWidth()+10)) {
                allergiesLayout.setVisibility(View.VISIBLE);
            }

            else
                allergiesLayout.setVisibility(View.GONE);

        }

但这个解决方案不起作用.

but this solution doesn't work.

推荐答案

可以使用提供的这个方法:getEllipsisCount

You can use this method provided: getEllipsisCount

Layout layout = textview1.getLayout();
if(layout != null) {
    int lines = layout.getLineCount();
    if(lines > 0) {
        int ellipsisCount = layout.getEllipsisCount(lines-1);
        if ( ellipsisCount > 0) {
            Log.d(TAG, "Text is ellipsized");
        } 
    } 
}

哪里可以通过getLineCount()获取线

where line could be obtained via getLineCount()

这篇关于检查textview是否在android中是椭圆的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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