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使用WebView来显示天气预报
- android ImageView 显示本地图片
- Android的Recyclerview的使用
- 【解疑答惑】—— android:maxLines="1"没满一行显示省略号,用and
- android 横竖屏判断
- Android(安卓)RecyclerView 设置显示滚动条属性后,滚动条不显示踩
- android xml控件属性
- Android(安卓)Edittext 显示光标 获取焦点 监听焦点
- Android(安卓)API中文文档EditText