android – 以编程方式在TextView中居中文本
作者:互联网
我以编程方式创建了一个TextView,文本和背景可绘制.我想将文本居中在TextView中,但文本居于顶部.
这是我在TextView中居中文本的代码:
protected class TileView extends TextView{
private int tileType;
private int col;
private int row;
protected TileView(Context context, int row, int col, int tileType) {
super(context);
this.col = col;
this.row = row;
this.tileType = tileType;
String result = Integer.toString(RandomNumber(1,9));
Drawable image = getResources().getDrawable(tile_id[tileType]);
setBackgroundDrawable(image);
setClickable(true);
setText(result);
setTextAppearance(context, R.style.boldText);
setOnClickListener(GameView.this);
}
}
这是我在onLayout()中的代码
int left = oneFifth * tileView.getCol();
int top = oneFifth * tileView.getRow();
int right = oneFifth * tileView.getCol() + oneFifth;
int bottom = oneFifth * tileView.getRow() + oneFifth;
tileView.layout(left, top, right, bottom);
tileView.setGravity(Gravity.CENTER);
如何在TextView中居中文本?
解决方法:
试试这个希望它可以帮助你
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
yourTextView.setGravity(Gravity.CENTER);
检查这个Link
标签:android,android-layout,android-textview 来源: https://codeday.me/bug/20190729/1569228.html