如何在ImageView上叠加GridLayout?

时间:2015-03-20 22:25:25

标签: android imageview grid-layout tic-tac-toe

我在Android上做一个tic tac toe只是为了好玩,我不知道如何设计它。事实上,我制作了一个网格布局,有9个交互方格,但我不知道如何将网格图片放在它下面。

首先我将网格放在背景属性上:

<GridLayout
    android:id="@+id/Game"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:columnCount="3"
    android:rowCount="3"
    android:numColumns="3"

    android:background="@drawable/grid"
    >

但它不起作用:

然后我尝试将Imageview作为gridLayout的父级,但这是不可能的。还有其他方法吗?

1 个答案:

答案 0 :(得分:0)

gridLayout或表格布局的概念是您在完成后设置网格,然后为每个网格元素添加子组件。对于你的TicTacToe板,我建议你添加9个ImageButtons,每个盒子一个。当用户单击该按钮时,您可以将图像从空白更改为X或O.

这是我从拨号中提取的一个示例,它具有类似的网格/表格概念。

       <ImageButton
            android:id="@+id/imageButton_dtmf_1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/btn_dial_1"
            android:contentDescription="@string/dialer_button_1" />

在java代码中,您将识别9个ImageButton中的每一个,并为每个ImageButtons设置一个点击监听器。

以下是GridLayout的基础知识:http://www.techotopia.com/index.php/Working_with_the_Android_GridLayout_in_XML_Layout_Resources