有时我们需要让textview换行后也居中显示
public class CenterTextView extends android.support.v7.widget.AppCompatTextView{ 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); initView(); } private void initView() { tp = new TextPaint(Paint.ANTI_ALIAS_FLAG); tp.setTextSize(getTextSize()); tp.setColor(getCurrentTextColor()); myStaticLayout = new StaticLayout(getText(), tp, getWidth(), Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false); } @Override protected void onDraw(Canvas canvas) { myStaticLayout.draw(canvas); } }