android中判断关于文本展示是否出现省略号的判断
来源:互联网 发布:亚马逊的大数据行动 编辑:程序博客网 时间:2024/05/18 04:54
对于一些文本展示类需求,经常会要求设置最大展示行数(maxLines),当文本长度大于要展示的最大内容时出现省略号,同时根据是否出现省略号做一些其他操作,比如显示"全文"等字眼,然后要求点击全文查看全部内容。这个时候就需要判断文本是否已经溢出最大展示量,下面提供自己的一种实现方法,这里在布局文件中设置TextView的最大行数为5,android:maxLines=5。
实现:
Layout layout = ((TextView) holder.getView(R.id.content)).getLayout();if (layout != null) {int lineCount = layout.getLineCount();//奇葩的小米note2居然会返回大于5(最大行数)的值if (lineCount > 5) {//小米note2的兼容处理holder.setVisibility(R.id.content_showfull, View.VISIBLE);} else {int ellipsisCount = layout.getEllipsisCount(lineCount - 1);//小米note2下返回0if (ellipsisCount > 0) {holder.setVisibility(R.id.content_showfull, View.VISIBLE);} else {holder.setVisibility(R.id.content_showfull, View.GONE);}}} else {ViewTreeObserver observer = holder.getView(R.id.content).getViewTreeObserver();if (observer.isAlive()) {observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {@Overridepublic boolean onPreDraw() {Layout layout = ((TextView) holder.getView(R.id.content)).getLayout();if (layout != null) {int lineCount = layout.getLineCount();if (lineCount > 5) {holder.setVisibility(R.id.content_showfull, View.VISIBLE);} else {int ellipsisCount = layout.getEllipsisCount(lineCount - 1);if (ellipsisCount > 0) {holder.setVisibility(R.id.content_showfull, View.VISIBLE);} else {holder.setVisibility(R.id.content_showfull, View.GONE);}}} else {holder.setVisibility(R.id.content_showfull, View.GONE);}try {(holder.getView(R.id.content).getViewTreeObserver()).removeOnPreDrawListener(this);} catch (Exception e) {e.printStackTrace();}return true;}});}}
0 0
- android中判断关于文本展示是否出现省略号的判断
- 判断Fragment是否展示
- Android TextView 判断文字内容是否超出显示省略号
- 判断文本中是否含有中文字符
- 判断文本中是否是数字
- XSLT中判断文本是否包含
- 判断图中是否出现“环”的算法
- 设置maxLines=2,ellipseSize=end,判断文本内容是否超出,显示省略号
- 如何判断Fragment是否展示
- ios判断是否展示轮播图
- 判断字符串中符号是否成对出现
- 关于iOS与android中简单的人脸识别(判断是否有人脸)
- 判断字符是否出现
- 判断字符是否出现
- Android中关于时间点的判断
- 关于Android添加、判断快捷方式是否已添加的问题
- 关于如何判断jQuery中对象是否存在的问题
- 关于android 如何判断是否连接网络
- android中Intent的使用
- Win10的UWP之标题栏的返回键(一)
- HDU 1598 find the most comfortable road
- 【Jenkins】包含dubbo服务的war包自动部署tomcat
- jzoj2931 书架
- android中判断关于文本展示是否出现省略号的判断
- Qt之QSequentialAnimationGroup
- mysql添加外键
- There was an internal API error.
- 生命周期方法
- Hbase分布式集群安装配置
- Android自定义控件:动画类----alpha、scale、translate、rotate、set的xml属性及用法
- 准备食物
- windows下curl的安装和简单使用