4x4 GridLayout在Android中以编程方式填充

时间:2016-11-23 12:30:43

标签: android layout margin android-gridlayout

我需要在Android上制作2048游戏。

对于Board,我们需要使用GridLayout并以编程方式设置它。

它将像原始游戏一样是4x4网格。

对于卡片,我们必须使用带有TextView的FrameLayout并将其居中。

Original game screenshot

My result screenshot

这就是我已经拥有的:

public class Board extends GridLayout {

    private final int BOARD_WIDTH = 4;
    private final int BOARD_HEIGHT = 4;

    private Card[][] cards;

    public Board(Context context) {
        super(context);

        initBoard();
    }

    public Board(Context context, AttributeSet attrs) {
        super(context, attrs);

        initBoard();
    }

    public Board(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initBoard();
    }

    public void initBoard() {

        this.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorBoard));
        this.setRowCount(BOARD_HEIGHT);
        this.setColumnCount(BOARD_WIDTH);

        Random random = new Random();

        cards = new Card[BOARD_WIDTH][BOARD_HEIGHT];

        for (int x = 0; x < BOARD_WIDTH; x++) {

            for (int y = 0; y < BOARD_HEIGHT; y++) {

                int n = random.nextInt(2);
                cards[x][y] = new Card(this.getContext(), (n * 2));
            }
        }
    }

    public void addCardsToBoard() {

        for (int x = 0; x < BOARD_WIDTH; x++) {

            for (int y = 0; y < BOARD_HEIGHT; y++) {

                GridLayout.LayoutParams param = new LayoutParams();
                param.height = LayoutParams.WRAP_CONTENT;
                param.width = LayoutParams.WRAP_CONTENT;
                param.setGravity(Gravity.CENTER);
                param.columnSpec = GridLayout.spec(x);
                param.rowSpec = GridLayout.spec(y);
                param.topMargin = 50;

                Card c = cards[x][y];
                c.setLayoutParams(param);

                this.addView(cards[x][y], 230, 230);
            }
        }

    }

}

-

public class Card extends FrameLayout {

    private int number = 0;
    private TextView txtNumber;

    public Card(Context context, int n) {
        super(context);

        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;

        txtNumber = new TextView(context);
        txtNumber.setTextSize(30);
        txtNumber.setLayoutParams(params);

        this.addView(txtNumber);

        setNumber(n);
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;

        this.setBackgroundColor(ContextCompat.getColor(getContext(), number == 0 ? R.color.colorCard : R.color.colorCardActive));
        txtNumber.setText(number == 0 ? "" : "" + number);
    }
}

我能做些什么让它看起来更好(更像是原版游戏)。

任何提示/建议表示赞赏。

1 个答案:

答案 0 :(得分:0)

为GridLayout添加水平和垂直间距,这适用于它的列和行。

view.setHorizontalSpacing(10); 
view.setVerticalSpacing(10);