您的位置:首页 > 其它

TextView 内容太多用省略号...,点击显示全部

2017-01-06 18:12 585 查看

1.在xml文件中控制TextView内容太多的时候用省略号表示

控制TextView内容太多的时候用省略号表示的属性:ellipsize

这个属性有5个值可选:

android:ellipsize = "end"    省略号在结尾
android:ellipsize = "start"   省略号在开头
android:ellipsize = "middle"     省略号在中间
android:ellipsize = "marquee"  跑马灯
android:ellipsize = "none"  不省略


在使用省略的时候,最好加一个TextView显示行数的约束

android:singleline=”true”或者

android:lines=”2”

2.在java文件中setEllipsize设置省略

TruncateAt是一个枚举类

tv.setEllipsize(TextUtils.TruncateAt.valueOf(“END”));

tv.setEllipsize(TextUtils.TruncateAt.valueOf(“START”));

tv.setEllipsize(TextUtils.TruncateAt.valueOf(“MIDDLE”));

tv.setEllipsize(TextUtils.TruncateAt.valueOf(“MARQUEE”));

最好加一个TextView显示行数的约束,例如:

tv.setSingleLine(true);
tv.setLines(lines)


不仅对于textview有此属性,对于editext也有,不过它不支持marquee。

package com.hp.classes.education.teacher.exam.util;

import android.content.Context;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.widget.TextView;

public class DiyTextView extends TextView {

static Typeface mTypeface; // 字体
private float maxWidth = 0.0f;
private boolean isSingleLine = true; // 是否是单行
private CharSequence myText = null;

public boolean getIsSingleLine() {
return isSingleLine;
}

@Override
public void setSingleLine(boolean singleLine) {
this.isSingleLine = singleLine;
super.setSingleLine(singleLine);
}

public DiyTextView(Context context) {
super(context, null);
setSingleLine();
this.setTypeface(getHpTTF(context));

}

public DiyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setSingleLine();
this.setTypeface(getHpTTF(context));
}

public DiyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setSingleLine();
this.setTypeface(getHpTTF(context));
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
float width = getTextOutWidth();
if (maxWidth != width && isSingleLine && myText != null) {
setText(myText);
}
}

/**
* @return 返回TextView可显示文本内容的实际长度
*/
private float getTextOutWidth() {
float width = getMeasuredWidth();
if (width != 0) {
float paddingLeft  =  getPaddingLeft();
float paddingRight =  getPaddingRight();
return (width - paddingLeft - paddingRight);
}
return 0;
}

@Override
public void setText(CharSequence text, BufferType type) {
TextPaint textPaint = this.getPaint();  //文字绘笔

maxWidth = getTextOutWidth();

if (maxWidth >= 0.1f && isSingleLine && text != null) {
//   有个内容过长加省略号的属性 TruncateAt内容截断的方式
TruncateAt truncateAt = getEllipsize();

if (truncateAt != TruncateAt.MARQUEE) {
CharSequence str = TextUtils.ellipsize(text, textPaint, maxWidth, truncateAt);
text = str;
}
}
myText = text;

super.setText(text, type);
}

/**
* @param context
* @return 从File或Assets中获取字体
*/
public static Typeface getHpTTF(Context context) {
//      String path = "/hpdata/fonts/hwapu-microhei+diy.ttf";
//      File file = new File(path);
//      if (file.exists()) {
//          Typeface.createFromFile(file);
//      }
//

if (mTypeface == null) {
try {
mTypeface = Typeface.createFromFile("/hpdata/fonts/hwapu-microhei+diy.ttf");
} catch (Exception e) {
e.printStackTrace();
}
}
if (mTypeface == null) {
try {
mTypeface = Typeface.createFromAsset(context.getApplicationContext().getAssets(),
"Hwapu-microhei+DIY.ttf");
} catch (Exception e) {
// TODO: handle exception
}
}

return mTypeface;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐