因为某些不知名bug,使得我在实现TextView单行超出部分显示为省略号的时候使用这个方法

android:maxLines="1"  android:ellipsize="end"

的时候,有时候只显示省略号不显示内容,有时候不显示省略号,因此笔者尝试自定义实现这个功能。这里举例20px的字体大小。

TextPaint textPaint = new TextPaint();textPaint.setTextSize(context.getResources().getDisplayMetrics().scaledDensity * 20);String displayStr = dataStr;if (textPaint.measureText(dataStr) > textView.getWidth()) {    while (textPaint.measureText(displayStr) >= (textView.getWidth() - textPaint.measureText("..."))) {        displayStr = displayStr.substring(0, displayStr.length() - 1);    }    displayStr += "...";}textView.setText(displayStr);

更多相关文章

  1. android webview字体大小的控制
  2. Android Studio设置界面风格和字体大小
  3. Android中如何让TextView显示指定的行数并且多出的部分显示省略
  4. android根据字体大小设置控件高度
  5. android默认字体大小、高度、宽度
  6. android ICS4.0.3 改变默认字体大小
  7. Android多屏幕适配之字体大小、行间距和字间距
  8. 设置TextView超过一定长度后显示省略号
  9. android中修改tablayout中的字体大小和颜色

随机推荐

  1. Android(安卓)的消息队列模型
  2. Android(安卓)framework Watchdog的监控
  3. Android夸进程通信机制二:Parcel 与 Parce
  4. Android(安卓)JNI(Java Native Interface)
  5. Android(安卓)上Camera分析
  6. Android(安卓)RelativeLayout属性
  7. 《Android/OPhone开发完全讲义》连载(4):And
  8. Android启动过程深入解析
  9. Android(安卓)* HandleMessage的模型
  10. Android的Camera架构介绍