Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在自定义视图组中垂直居中放置按钮中的文本_Android_Button_Viewgroup - Fatal编程技术网

Android 如何在自定义视图组中垂直居中放置按钮中的文本

Android 如何在自定义视图组中垂直居中放置按钮中的文本,android,button,viewgroup,Android,Button,Viewgroup,我正在构建一个自定义视图组GridViewGroup,以模拟一个简单的GridLayout(我不能使用它,因为我的目标Android版本不支持它)。在网格中,我想要一个GridButton的数组(GridButton是一个简单的扩展按钮)。它工作得很好:我可以构建任意大小的网格,GridButton可以适当地调整大小。但是,我不能使文本垂直居中于GridButton的中心位置——水平重力是值得尊重的,但垂直重力似乎不是 我尝试在按钮上使用LayoutParams,但ViewGroup Layou

我正在构建一个自定义视图组GridViewGroup,以模拟一个简单的GridLayout(我不能使用它,因为我的目标Android版本不支持它)。在网格中,我想要一个GridButton的数组(GridButton是一个简单的扩展按钮)。它工作得很好:我可以构建任意大小的网格,GridButton可以适当地调整大小。但是,我不能使文本垂直居中于GridButton的中心位置——水平重力是值得尊重的,但垂直重力似乎不是

我尝试在按钮上使用LayoutParams,但ViewGroup LayoutParams不支持重力(至少没有setGravity()方法)

我希望我错过了一些明显的东西

以下是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);
        }

    }
提前感谢您的帮助

我正在构建一个自定义视图组GridViewGroup,以模拟一个简单的 GridLayout(我不能使用它,因为我的目标Android版本 不支持它)


我以前没有使用过它,但我读到最新版本的Android兼容性库包含GridLayout。

的确如此!GridLayout已经在Android支持库中向后移植到API 7及以上版本。更多信息。谢谢你的信息。
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);
    }
}