How do I specify width and height of Android GridLayout cells in relative measures?

前端 未结 5 1871
自闭症患者
自闭症患者 2020-12-13 13:01

I\'m trying to figure out how GridLayout works, but one thing I can\'t figure out from the documentation is how or if one can control the size of the grid cells.

Sa

5条回答
  •  粉色の甜心
    2020-12-13 13:27

    There is a helper method inside the GridLayout:

    GridLayout.LayoutParams params = new GridLayout.LayoutParams();
                params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1, getAlignment(Gravity.NO_GRAVITY, true), 1);
    item.setLayoutParams(params);
    
    private GridLayout.Alignment getAlignment(int gravity, boolean horizontal) {
            int mask = horizontal ? HORIZONTAL_GRAVITY_MASK : VERTICAL_GRAVITY_MASK;
            int shift = horizontal ? AXIS_X_SHIFT : AXIS_Y_SHIFT;
            int flags = (gravity & mask) >> shift;
            switch (flags) {
                case (AXIS_SPECIFIED | AXIS_PULL_BEFORE):
                    return horizontal ? LEFT : TOP;
                case (AXIS_SPECIFIED | AXIS_PULL_AFTER):
                    return horizontal ? RIGHT : BOTTOM;
                case (AXIS_SPECIFIED | AXIS_PULL_BEFORE | AXIS_PULL_AFTER):
                    return FILL;
                case AXIS_SPECIFIED:
                    return CENTER;
                case (AXIS_SPECIFIED | AXIS_PULL_BEFORE | GravityCompat.RELATIVE_LAYOUT_DIRECTION):
                    return START;
                case (AXIS_SPECIFIED | AXIS_PULL_AFTER | GravityCompat.RELATIVE_LAYOUT_DIRECTION):
                    return END;
                default:
                    return GridLayout.FILL;
            }
        }
    

    Normally this is only accessible thought reflection.

    And the smaller version:

    params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1, GridLayout.FILL, 1);
    

提交回复
热议问题