Android的GridView控件的井字棋控件、Android、GridView、井字棋

2023-09-06 06:53:19 作者:12.这他妈才是爱情°

您好,

我试图让一个井字棋游戏作为一个学习锻炼的Andr​​oid应用程序决策。

要学习使用的GridView,我已经用它设置了一个3×3的游戏板(填入imageViews)。我创建了一个自定义的适配器,它正确地加载imageViews到GridView的,但我不知道如何确保每个单元格的宽度将收缩足够小,只适合图像(所以它看起来像一个3×3格,所有的细胞正方形)。

谁能帮我这个?

下面是我的XML为GridView:

 < GridView的机器人:ID =@ + ID /板
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:为numColumns =3
/>
 
android控件学习之十五 GridView实例

解决方案

您可能需要指定ImageViews如何,当你在适配器创建它们要绘制。

如果你看到的例子在的开发指南他们指定的图像如何被缩放:

 的ImageView ImageView的;
 ...
 ImageView的=新ImageView的(mContext);
 imageView.setLayoutParams(新GridView.LayoutParams(85,85));
 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
 

有不同的缩放选项,其中包括'无',这可能是合适的。也有改变,他们是如何与渲染的可能性

  imageView.setAdjustViewBounds(真正的);
 

属性,但如何将这些不同的配置进行交互仍然是相当的试错我。我希望这有助于:)

HI,

I'm trying to make a tic-tac-toe game as a learning exercise in Android app making.

To learn to use gridView, I have used it to set out a 3 x 3 game board (filled in with imageViews). I have created a custom adapter which correctly loads the imageViews into the gridView, but I do not know how to make sure each cell's width shrinks small enough to just fit the image (so it looks like a 3 x 3 grid where all the cells are squares).

Can anyone help me out with this?

Here's my XML for the gridView:

<GridView android:id="@+id/board"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numColumns="3"
/>

解决方案

You will probably need to specify how the ImageViews are to be drawn when you create them in the adapter.

If you see the example at the dev guide they specify how the images are to be scaled:

 ImageView imageView;
 ...
 imageView = new ImageView(mContext);
 imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

There are different scaling options, including 'none' which might be suitable. There is also the possibility of changing how they are rendered with the

  imageView.setAdjustViewBounds(true);

attribute, but how these different configurations interact is still fairly trial and error for me. I hope this helps :)