StaticLayout 貌似我们平常用的不是特别的,但有时候需要特殊处理的时候还是会用到
StaticLayout(
CharSequence source, 需要分行的字符串
int bufstart, 分行的字符串从第几的位置开始
int bufend, 分行的字符串从第几的位置结束
TextPaint paint,
int outerwidth, 宽度,字符串超出宽度时自动换行
Alignment align, 有ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE 三种
float spacingmult, 相对行间距,相对字体大小,1.5f表示行间距为1.5倍的字体高度
float spacingadd,. 在基础行距上添加多少实际行间距等于这两者的和。
boolean includepad,
TextUtils.TruncateAt ellipsize, 从什么位置开始省略
int ellipsizedWidth 超过多少开始省略需要指出的是这layout是默认画在Canvas的(0,0)点的,如果需要调整位置只能在draw前移Canvas的起始坐标canvas.translate(x,y);
)
这个就网上的 , 让textview 换行的多余部分居中
public class CenterTextView extends TextView{
private StaticLayout myStaticLayout;
private TextPaint tp;
public CenterTextView(Context context, AttributeSet attrs){
super(context, attrs);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
init();
}
private void init() {
tp = new TextPaint(Paint.ANTI_ALIAS_FLAG);
tp.setTextSize(getTextSize());
tp.setColor(getCurrentTextColor());
myStaticLayout = new StaticLayout(getText(), tp, getWidth(), Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
}
@Override
protected void onDraw(Canvas canvas)
{
myStaticLayout.draw(canvas);
}
}
StaticLayout 多用于 我们自定义 View 中需要处理一些东西的地方
用法很简单,