如何最好地在Android应用程序/游戏中显示4x4图像的网格? [英] How best to display a grid of 4x4 images in an Android app/game?
问题描述
我目前正在为大学作业创建一个打地鼠"型游戏,我需要知道如何最好地在屏幕上显示孔.我看过GridView,GridLayout和TableLayout,由于我以后需要从每个孔弹出各种可单击项,因此我对应该选择哪一个感到困惑.总体屏幕应该只在顶部有一个标语,上面会显示得分,生活等,在底部有一个暂停按钮,中间有一个4x4网格,用于显示孔.我是XML和Java的新手,所以任何建议都将不胜感激.
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.
推荐答案
我认为,使用GridLayout很容易,但是使用SDK> 14则有局限性.
I think, easy enough with GridLayout, but then you have limitation of using SDK > 14.
轻松编写4行代码即可制作4 x 4网格:
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()));
}
上面的
代码仅供参考.可能您需要根据需要更改图像,尺寸."YOUR_OWN_VIEW"-更改为您的视图名称.
code above is for reference. May you need to change the images, size per your need. "YOUR_OWN_VIEW" - change to your view name.
这篇关于如何最好地在Android应用程序/游戏中显示4x4图像的网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!