我们知道,要使TextView单行显示,如果超出的话用“…”显示,只要设置

android:ellipsize="end"android:singleLine="true"

或者多行显示,超出的部分使用“…”显示,那么,设置如下

android:lines="2"android:ellipsize="end"

那么问题是,我们怎么知道TextView已经省略了内容呢?

        TextView textView = (TextView) findViewById(R.id.txt_course_introduce);        Layout l=textView.getLayout();        if(l!=null){            int lines =l.getLineCount();            LogUtils.d(TAG, "lines = " + lines);            if(lines>0){                if(l.getEllipsisCount(lines-1)>0){                    LogUtils.d(TAG, "Text is ellipsized");                }            }        }else {            LogUtils.d(TAG,"Layout is null");        }

通过textview获取Layout,然后根据Layout的一个方法getEllipsisCount(int),来判断是否已经省略,但Layout大多时候获取到的都是null,为什么呢?原因是,Layout要等TextView绘制完了才能够拿到Layout的对象。

final TextView textView = (TextView) findViewById(R.id.txt_training_introduce);        textView.post(new Runnable() {            @Override            public void run() {                Layout l = textView.getLayout();                if (l != null) {                    int lines = l.getLineCount();                    LogUtils.d(TAG, "lines = " + lines);                    if (lines > 0) {                        if (l.getEllipsisCount(lines - 1) > 0) {                            LogUtils.d(TAG, "Text is ellipsized");                        }                    }                } else {                    LogUtils.d(TAG, "Layout is null");                }            }        });

更多相关文章

  1. android 系统状态栏的隐藏和显示
  2. android 界面布局-各个布局的属性介绍
  3. android 布局特殊的属性介绍
  4. Android(安卓)EditText属性总结
  5. 如何让Android中的控件显示居中?
  6. Android(安卓)Studio如何显示行号
  7. Android用户界面 UI组件--TextView及其子类(一) TextView
  8. [每日100问][2011-10-01]iphone开发笔记,今天你肿了么
  9. android imageView 图片显示方式属性

随机推荐

  1. 利用HTML canvas制作酷炫星星坠地粒子特
  2. html5: 新特性(表单)
  3. CHtmlEditCtrl (3): More HTML Editor Op
  4. JQuery函数不能用于初始触发器
  5. 百度地图Api进阶教程-实例高级操作8.html
  6. HTML5新增标签与属性
  7. 如何确定在web页面上呈现的字符串的长度(
  8. telnet建立http连接获取网页HTML内容
  9. jQuery:流体同位素仅在调整大小后才工作
  10. html中显示div的时候,超出浏览器的宽,怎么