Android棋盘式布局层次结构

3
我正在使用Android视图创建棋盘。
Java代码: 我有一个Board对象,其包含一个由64个Cell对象组成的数组。每个Cell对象都有所需的背景图像和可选的前景图像。我正在尝试想出一个好的方法来绘制这个棋盘。
Android代码: 我的进展如下: - 每个单元格都有一个drawSelf函数,返回一个FrameLayout,其中包含两个子图像视图以显示两个图像。 - board.xml具有gridlayout作为根布局。
我已经尝试过构建这个结构,但是当我尝试将FrameLayout添加到面板上时,我遇到了各种错误,而且我也无法保持FrameLayout在GridLayout的网格线内。
注意: 我不一定要使用这种数据结构,我希望最终能够以一种方式绘制8x8的棋盘,每个单元格的宽度和高度都是正方形棋盘的1/8,每个单元格都可以显示两个图像,具有ID并触发一个onClick(self.id)调用到我的游戏逻辑。
谢谢。

你还在做这个吗? - mr5
@mr5 如果你有任何见解,我很乐意听取。 - M Y
1个回答

0

我其实是为了扩展自己在Android开发方面的知识而在做同样的事情。我正在尝试一种方法,即使用Framelayout,其中包含8个LinearLayout,每个LinearLayout都包含8个带有ImageView的LinearLayout。这种方法可以为我绘制所有内容,并且我可以免费获得onclick事件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/car_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66ccff"
tools:context=".Activity_Main" >

<FrameLayout
    android:id="@+id/checker_frame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="false" >

    <LinearLayout
        android:layout_width="315dp"
        android:layout_height="300dp"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/square_1_1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="#FFFFFF"
                android:onClick="squareOnClick" />

            <ImageView
                android:id="@+id/square_1_2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="#000000"
                android:onClick="squareOnClick" />

            <ImageView
                android:id="@+id/square_1_3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="#FFFFFF"
                android:onClick="squareOnClick" />

            <ImageView
                android:id="@+id/square_1_4"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="#000000"
                android:onClick="squareOnClick" />

            <ImageView
                android:id="@+id/square_1_5"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="#FFFFFF"
                android:onClick="squareOnClick" />

            <ImageView
                android:id="@+id/square_1_6"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="#000000"
                android:onClick="squareOnClick" />

            <ImageView
                android:id="@+id/square_1_7"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="#FFFFFF"
                android:onClick="squareOnClick" />

            <ImageView
                android:id="@+id/square_1_8"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:background="#000000"
                android:onClick="squareOnClick" />
        </LinearLayout>

        --Repeat Linear layouts

你可以看到我在所有的ImageView上都有一个squareOnClick调用方法。每次点击都会触发这个监听器。我可以通过它的ID名称来识别方块的ID,从而对其进行反应:

public void squareOnClick(View view)
{
    String idName = getResources().getResourceEntryName(view.getId());
    idName = idName.replace("square_", "");

    String[] coordinate = idName.split("_");
    if(coordinate.length != 2)
        return;

    int x = Integer.parseInt(coordinate[0]);
    int y = Integer.parseInt(coordinate[1]);
    Log.i("Activity_Checker", "Coordinate pressed: " + x + ":" + y);

    TextView tv = (TextView) findViewById(R.id.statustextview);
    tv.setText("X:" + x + " Y:" + y);
}

我正在考虑自由绘制一切的想法,但只有在我无法解决其他问题时才会这样做,这些问题包括:

  • 正确重新定位帧布局
  • 正确调整帧布局大小
  • 跨布局进行属性动画

这就是我目前的进展。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接