簡體   English   中英

如何在自定義視圖組的按鈕中垂直居中放置文本

[英]How to Vertically Center Text in Button in Custom ViewGroup

我正在構建一個自定義的ViewGroup GridViewGroup,以模擬一個簡單的GridLayout(因為我的目標Android版本不支持它,所以無法使用它)。 在網格中,我需要一個GridButton的數組(一個GridButton是一個簡單的擴展Button)。 它運行得很好:我可以構建任意大小的網格,並且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);
        }

    }

...這是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);
    }
}

在此先感謝您的幫助!

我正在構建一個自定義的ViewGroup GridViewGroup,以模擬一個簡單的GridLayout(因為我的目標Android版本不支持它,所以無法使用它)。

我以前沒有使用過它,但是我讀到最新版本的Android兼容性庫包含GridLayout。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM