У меня есть пользовательский вид с четырьмя кнопками.Я создал представление в Java, потому что я хочу анимировать кнопки.Я хочу центрировать текст на кнопках, но я могу сделать это только по горизонтали (слева направо), но не по вертикали.Текст прилипает к вершине.
Вот самый основной код, который я мог бы получить:
public class CopyOfGameView extends ViewGroup implements View.OnClickListener {
private Main main;
public CopyOfGameView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CopyOfGameView(Context context) {
super(context);
}
public void reset(Main main) {
this.main = main;
removeAllViews();
for (int c = 0; c < 4; c++) {
ButtonView buttonView = new ButtonView(getContext(), c);
buttonView.setText("x");
buttonView.setTextSize(6);
buttonView.setGravity(Gravity.CENTER);
addView(buttonView);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int w = getWidth();
int h = getHeight();
int oneFourth = h / 4;
int count = getChildCount();
for (int i = 0; i < count; i++) {
ButtonView buttonView = (ButtonView) getChildAt(i);
int left = 0;
int top = oneFourth * buttonView.getRow();
int right = w;
int bottom = oneFourth * buttonView.getRow() + oneFourth;
buttonView.layout(left, top, right, bottom);
}
}
public void onClick(View v) {}
protected ButtonView findButtonView(int row) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
View v = getChildAt(i);
if (v instanceof ButtonView) {
ButtonView buttonView = (ButtonView) v;
if (buttonView.getRow() == row) {
return buttonView;
}
}
}
return null;
}
protected class ButtonView extends Button {
private int row;
protected ButtonView(Context context, int row) {
super(context);
this.row = row;
Drawable image = getResources().getDrawable(R.drawable.btn_black);
setBackgroundDrawable(image);
setClickable(true);
setOnClickListener(CopyOfGameView.this);
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
}
}
Это нормально или это ошибка в Android?Как я могу центрировать текст по вертикали?