How best to display a grid of 4x4 images in an And

2019-08-31 12:03发布

I am currently creating a whack-a-mole type game for a university assignment and I need to know how best to display the holes on screen. I have had a look at GridView, GridLayout and TableLayout and am confused as to which one I should pick considering I later need to make various clickables pop up from each hole. The Overall screen should simply have a banner at the top that will display score, lives etc. and a pause button at the bottom with a 4x4 grid in the middle displaying the holes. I am relatively new to XML and Java so any advice would be greatly appreciated.

1条回答
你好瞎i
2楼-- · 2019-08-31 12:52

I think, easy enough with GridLayout, but then you have limitation of using SDK > 14.

Easy 4 lines of code to make 4 x 4 grid:

        gridContainer = new GridLayout(this);
        gridContainer.setColumnCount(4);
        YOUR_OWN_VIEW.addView(gridContainer);

        for(int i = 0; i < 16; i++) 
        {
            ImageView img = new ImageView(this);
            img.setImageDrawable(this.getResources().getDrawable(R.drawable.ic_launcher));

            gridContainer.addView(img, Math.max(0, gridContainer.getChildCount()));
        }

code above is for reference. May you need to change the images, size per your need. "YOUR_OWN_VIEW" - change to your view name.

查看更多
登录 后发表回答