How best to display a grid of 4x4 images in an Android app/game?

﹥>﹥吖頭↗ 提交于 2019-12-11 19:33:22

问题


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 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.



来源:https://stackoverflow.com/questions/21052224/how-best-to-display-a-grid-of-4x4-images-in-an-android-app-game

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!