Android(安卓)TextView 如何判断是否已经省略
16lz
2021-12-04
我们知道,要使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"); } } });
更多相关文章
- android 系统状态栏的隐藏和显示
- android 界面布局-各个布局的属性介绍
- android 布局特殊的属性介绍
- Android(安卓)EditText属性总结
- 如何让Android中的控件显示居中?
- Android(安卓)Studio如何显示行号
- Android用户界面 UI组件--TextView及其子类(一) TextView
- [每日100问][2011-10-01]iphone开发笔记,今天你肿了么
- android imageView 图片显示方式属性