1.设置行间距
android:lineSpacingMultiplier="1.8"
2.字间距设置(需要自定义控件)
import android.graphics.*;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by mrni-mac on 14-11-25.
*/
public class MyTextView extends TextView {
private String content;
private int width;
private Paint paint;
private int xPadding;
private int yPadding;
private int textHeight;
private int xPaddingMin;
int count;
int[][] position;
public MyTextView(Context context) {
super(context);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void setText(String str) {
width = this.getWidth();
getPositions(str);
this.invalidate();
}
public void init() {
paint = new Paint();
paint.setColor(Color.parseColor("#888888"));
paint.setTypeface(Typeface.DEFAULT);
paint.setTextSize(dip2px(this.getContext(), 14f));
Paint.FontMetrics fm = paint.getFontMetrics(); //
textHeight = (int) (Math.ceil(fm.descent - fm.top) + 2); //
xPadding = dip2px(this.getContext(), 4f);
yPadding = dip2px(this.getContext(), 10f);
xPaddingMin = dip2px(this.getContext(), 2f);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!TextUtils.isEmpty(content)) {
for (int i = 0; i < count; i++) {
canvas.drawText(String.valueOf(content.charAt(i)),
position[i][0], position[i][1], paint);
}
}
}
public void getPositions(String content) {
this.content = content;
char ch;
int x = 0;
int lineNum = 1;
count = content.length();
position = new int[count][2];
for (int i = 0; i < count; i++) {
ch = content.charAt(i);
String str = String.valueOf(ch);
Rect rect = new Rect();
paint.getTextBounds(str, 0, 1, rect);
int strwidth = rect.width();
if (str.equals("?") || str.equals("?")) {
strwidth += (xPaddingMin * 2);
}
float textWith = strwidth;
x += textWith;
if (x > width) {
lineNum++; //
x = 0;
} else {
x -= textWith;
}
position[i][0] = x;
position[i][1] = (textHeight * lineNum) +
(yPadding * (lineNum - 1));
if (isNumOrLetters(str)) {
x += (textWith + xPaddingMin);
} else {
x += (textWith + xPadding);
}
}
this.setHeight((textHeight + yPadding) * lineNum);
}
public boolean isNumOrLetters(String str) {
String regEx = "^[A-Za-z0-9_]+$";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.matches();
}
public static int dip2px(Context context, float dip) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) ((dip * scale) + 0.5f);
}
}
3.字水平方向缩放
android:textScaleX="1.5"
4.内容省略
android:ellipsize="end" 省略号在结尾
android:ellipsize="start" 省略号在开头
android:ellipsize="middle" 省略号在中间
5.内容滚动展示(跑马灯)
android:ellipsize="marquee" 跑马灯
6.内容的收起及展开功能
//默认展示三行
android:lines="3"
//结尾显示省略号
android:ellipsize="end"
//以上两属性为内容的收起效果
//代码实现
TextView.setLines(3);
TextView.setEllipsize(TextUtils.TruncateAt.END);
//内容展开
//消除省略号
TextView.setEllipsize(null);
//消除lines=="3"这个固定值
TextView.setSingleLine(false);
7.设置小图标
xml设置
android:drawableLeft=""
android:drawableTop=""
android:drawableRight="@mipmap/jiaotou_xuanzexiangmu_xia"
android:drawableBottom=""
代码动态设置
Drawable drawableRightUP = getResources().getDrawable(R.mipmap.jiaotou_xuanzexiangmu_shang);
//left小图标放在左边
//top小图标放在上面
//right小图标放在右边
//bottom小图标放在下面
textView.setCompoundDrawablesWithIntrinsicBounds(null, null, drawableRightUP, null);