Textview内容过多,尾部显示省略号或【更多】
来源:互联网 发布:linux下启动tomcat 编辑:程序博客网 时间:2024/06/05 03:39
一先看图
二、TextView 内容过多,直接设置 maxLines 和ellipsize=end 效果如图 上方TextView
三、尾部添加[更多内容],一开始想通过相对布局实现,发现不太可能(若有解决方案欢迎指正)
四、解决方案:
public class AD01 extends Activity { TextView tv; String str = "举个例子来说明一吧,为了让大家更明白一点,比如一个铅笔盒中有一支笔,但在没有打开之前你并不知道它是什么笔,可能是铅笔也可能是钢笔,这里有两种可能,那么你就可以定义一个枚举类型来表示它"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ad02); tv = (TextView) findViewById(R.id.tttt); tv.setText(str); ViewTreeObserver observer = tv.getViewTreeObserver(); observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { ViewTreeObserver obs = tv.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); if (tv.getLineCount() > 2) { int lineEndIndex = tv.getLayout().getLineEnd(1); String html = tv.getText().subSequence(0, lineEndIndex -6) + "……<font color='#ff0000'>[更多内容]</font>"; tv.setText(Html.fromHtml(html)); } } }); }}
QQ群里小伙伴idea 写了一个自定义控件
public class AutoTextView extends TextView {
private int tagTextColor = 0xFFFF0000;
private String tag = "...[更多内容]";
public AutoTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
ViewTreeObserver viewTreeObserver = getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(this);
setText(getText().toString());
}
public AutoTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoTextView(Context context) {
this(context, null);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
public boolean onPreDraw() {
replaceText();
return super.onPreDraw();
}
public void replaceText() {
int count=getLineCount();
if(count>2){
int st=getLayout().getLineEnd(1);
String content = getText().toString().substring(0,st);
Paint paint = new Paint();
paint.setTextSize(getTextSize());
float pointWidth = paint.measureText(tag);
char[] textCharArray = content.toCharArray();
float drawedWidth = 0;
float charWidth;
for (int i = textCharArray.length-1; i >0 ; i--) {
charWidth = paint.measureText(textCharArray, i, 1);
if (drawedWidth < pointWidth ) {
drawedWidth += charWidth;
} else {
content=content.substring(0,i)+tag;
break;
}
}
setColor(content, content.length()-6, content.length(), tagTextColor);
}
}
private void setColor(String content, int start, int end, int textColor) {
if (start <= end) {
SpannableStringBuilder style = new SpannableStringBuilder(content);
style.setSpan(new ForegroundColorSpan(textColor), start, end,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
setText(style);
}
}
}
- Textview内容过多,尾部显示省略号或【更多】
- 完成内容过多 使用省略号来显示
- Android TextView 内容过长显示省略号
- android TextView设置字过多长长度后面显示省略号失效
- Android TextView内容过长加省略号,点击显示全部内容
- Android TextView内容过长加省略号,点击显示全部内容
- android 之 TextView内容不完全显示,省略号功能~~~
- Android TextView内容过长加省略号,点击显示全部内容
- Android TextView内容过长加省略号,点击显示全部内容
- Android TextView内容过长加省略号,点击显示全部内容
- Android 中TextView内容过长加省略号,点击显示全部内容
- Android TextView 判断文字内容是否超出显示省略号
- Android TextView内容过长加省略号,点击显示全部内容
- Android TextView内容过长加省略号,点击显示全部内容
- TextView内容过长加省略号,点击显示全部内容
- Android TextView内容过长加省略号,点击显示全部内容
- TextView 内容太多用省略号...,点击显示全部
- TextView限制多行,超出内容时用省略号显示
- 计算两个经纬度之间的距离
- CentOS安装jdk
- Java日志框架slf4j、jcl、jul、log4j1、log4j2、logback大总结
- Object-C中编写省略参数的多参数函数
- Yii1.1中的关联查询
- Textview内容过多,尾部显示省略号或【更多】
- 芒果iOS开发之ARC和MRC混编
- 【集群】EHCache分布式缓存集群环境配置
- Android 文件IO总结
- python学习之函数
- 安卓软件测试的几个要点
- 解决ecshop后台频繁退出问题
- ListView与RadioButton组合实现单选列表
- git rebase简介(基本篇)