[英]How to Vertically Center Text in Button in Custom ViewGroup
I am building a custom ViewGroup, GridViewGroup, to emulate a simple GridLayout (which I can't use because my target Android version doesn't support it). 我正在构建一个自定义的ViewGroup GridViewGroup,以模拟一个简单的GridLayout(因为我的目标Android版本不支持它,所以无法使用它)。 In the grid, I want an array of GridButton's (a GridButton is a simply extended Button). 在网格中,我需要一个GridButton的数组(一个GridButton是一个简单的扩展Button)。 It's working pretty well: I can build a grid of whatever size I want, and the GridButton's properly resize themselves. 它运行得很好:我可以构建任意大小的网格,并且GridButton可以自行调整大小。 However, I cannot make the text vertically center in the GridButton's -- horizontal gravity is honored, but it doesn't appear that vertical gravity is. 但是,我不能使文本在GridButton的垂直居中对齐-尊重水平重力,但似乎没有垂直重力。
I've tried using LayoutParams on the buttons, but the ViewGroup LayoutParams don't support gravity (at least there is no setGravity() method). 我尝试在按钮上使用LayoutParams,但是ViewGroup LayoutParams不支持重力(至少没有setGravity()方法)。
I hope I'm missing something obvious. 我希望我缺少明显的东西。
Here's the code for GridViewGroup... 这是GridViewGroup的代码...
public class GridViewGroup extends ViewGroup {
protected int rowCount;
protected int columnCount;
public GridViewGroup(Context context) {
super(context);
rowCount = 4;
columnCount = 4;
}
public GridViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GridViewGroup);
rowCount = a.getInt(R.styleable.GridViewGroup_rowCount, 1);
columnCount = a.getInt(R.styleable.GridViewGroup_columnCount, 1);
a.recycle();
}
@Override
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
int pl = getPaddingLeft();
int pt = getPaddingTop();
int pr = getPaddingRight();
int pb = getPaddingBottom();
int cellSize1 = (right - left + 1 - pl - pr) / rowCount;
int cellSize2 = (bottom - top + 1 - pt - pb) / columnCount;
if (cellSize2 < cellSize1)
cellSize1 = cellSize2;
Log.i("GridViewGroup", "onLayout: " + String.valueOf(left) + "," + String.valueOf(top) + "," + String.valueOf(right) + "," + String.valueOf(bottom) + "," + String.valueOf(cellSize1));
for (int i = 0; i < getChildCount(); i++) {
GridButton gb = (GridButton)getChildAt(i);
int l = left + pl + gb.layout_column * cellSize1;
int t = top + pt + gb.layout_row * cellSize1;
int r = l + gb.layout_columnSpan * cellSize1 - 1;
int b = t + gb.layout_rowSpan * cellSize1 - 1;
gb.setTextSize(TypedValue.COMPLEX_UNIT_PX, cellSize1 * 0.25f);
gb.setGravity(Gravity.CENTER);
gb.layout(l, t, r, b);
}
}
... and here's the code for GridButton ... ...这是GridButton的代码...
public class GridButton extends Button {
protected int layout_row;
protected int layout_column;
protected int layout_rowSpan;
protected int layout_columnSpan;
public GridButton(Context context) {
super(context);
layout_row = 0;
layout_column = 0;
layout_rowSpan = 1;
layout_rowSpan = 1;
}
public GridButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GridButton);
layout_row = a.getInt(R.styleable.GridButton_layout_row, 0);
layout_column = a.getInt(R.styleable.GridButton_layout_column, 0);
layout_rowSpan = a.getInt(R.styleable.GridButton_layout_rowSpan, 1);
layout_columnSpan = a.getInt(R.styleable.GridButton_layout_columnSpan, 1);
}
}
Thanks in advance for your help! 在此先感谢您的帮助!
I am building a custom ViewGroup, GridViewGroup, to emulate a simple GridLayout (which I can't use because my target Android version doesn't support it). 我正在构建一个自定义的ViewGroup GridViewGroup,以模拟一个简单的GridLayout(因为我的目标Android版本不支持它,所以无法使用它)。
I haven't used it before, but I read that the most recent version of the Android compatibility library contains a GridLayout. 我以前没有使用过它,但是我读到最新版本的Android兼容性库包含GridLayout。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.