java 如何在 Android 中创建 10x10 网格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14093642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a 10x10 grid in Android?
提问by UserBruiser
I am creating a small Battleship game and for the Android UI I was hoping to create a 10x10 grid of image buttons or something (anything easy). I was following the tutorial provided at this link
我正在创建一个小型战舰游戏,对于 Android UI,我希望创建一个 10x10 的图像按钮网格或其他东西(任何简单的东西)。我正在按照此链接提供的教程进行操作
My problem is that I can't get the images to display in a proper 10x10 format. Using the GridView, I set the num_columns to "10" in the XML but there is no such attribute for the number of rows. As phone sizes differ, is there a way to have a 10x10 grid auto fit any screen?
我的问题是我无法以正确的 10x10 格式显示图像。使用 GridView,我在 XML 中将 num_columns 设置为“10”,但没有这样的行数属性。由于手机尺寸不同,有没有办法让 10x10 网格自动适合任何屏幕?
By following the steps in the above link I can display 100 clickable images but my problem is with the formatting. Here's a screenshot of said result:
按照上述链接中的步骤,我可以显示 100 张可点击的图像,但我的问题在于格式。这是上述结果的屏幕截图:
I've tried changing values in XML to scale the images down but cannot figure out how to. Apologies for lack of code snippets but I really haven't altered much from the sample given in the above link other than the vertical and horizontal spacing.
我尝试更改 XML 中的值以缩小图像,但不知道如何操作。对缺少代码片段表示歉意,但除了垂直和水平间距之外,我真的没有从上面链接中给出的示例中改变太多。
Would appreciate any enlightenment, even if it's just to say I am I going about this the wrong way completely.
感谢您的启发,即使只是说我完全以错误的方式处理这个问题。
回答by Snicolas
You can achieve this with a table layout : http://www.mkyong.com/android/android-tablelayout-example/
您可以使用表格布局来实现这一点:http: //www.mkyong.com/android/android-tablelayout-example/
GridViews, just as ListViews have been designed to contain any number of rows.
GridViews,就像 ListViews 被设计为包含任意数量的行一样。
But I really doubt you want to use views in your game and not only use a canvas, draw anything and detect clicks without relying on views built-in mechanisms. It's more work but you end with something that will make you much more free to do what you want (for instance not having to deal with the number of rows of a container...)
但我真的怀疑您是否想在游戏中使用视图,而不仅仅是使用画布、绘制任何内容和检测点击,而不依赖于视图内置机制。这是更多的工作,但你最终会得到一些让你更自由地做你想做的事情(例如不必处理容器的行数......)
回答by Zohaib
go to this linkand try 2nd example Custom Adapter example. just change android:numColumns="auto_fit" to android:numColumns="10".
转到此链接并尝试第二个示例自定义适配器示例。只需将 android:numColumns="auto_fit" 更改为 android:numColumns="10"。
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:numColumns="10"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
回答by Ted Hopp
You can probably do what you want with a GridLayout
, but not with a GridView
(which automatically scrolls vertically). Using a GridLayout
, set all the buttons to have 0dp layout width and height, and set their layout gravity to fill
.
你可以用 a 做你想做的事GridLayout
,但不能用 a GridView
(它会自动垂直滚动)。使用 a GridLayout
,将所有按钮设置为 0dp 布局宽度和高度,并将其布局重力设置为fill
。