2013-06-26 38 views
3

我正在使用android视图创建棋盘格。用于棋盘的Android布局层次结构

java: 我有一个Board对象,其中包含64个Cell对象的数组。每个Cell对象都有一个必需的背景图像和一个可选的前景图像。我试图想出一个很好的方式来绘制这个。

了Android:

我到目前为止有: - 每个单元都有一个返回的FrameLayout,与两个图像的两个子图像视图的drawSelf功能。 - board.xml以root用户身份具有网格布局。我试图使这个结构,但我得到各种错误,当我尝试将FrameLayouts添加到板,并且我也有保持由GridLayout的网格线限制FrameLayouts的问题。

注: 我不依赖于这种数据结构,我想作为一个最终的结果是一些方法来绘制的1/8的方板的宽度和高度细胞的8x8板,带每个单元都能够显示两个图像,保存一个ID,并触发一个onClick(self.id)调用我的游戏逻辑。

谢谢。

+0

你还在这吗? – mr5

+0

@ mr5我是如果你有任何见解。 – hellyale

回答

0

我实际上是为了好玩而构建相同的东西,以扩展我在android开发方面的知识。我正在尝试一种方法,在该方法中我有一个Framelayout,其中包含8个LinearLayouts,每个LinearLayouts包含8个带有ImageView的LinearLayouts。这种方法为我画了一切,再加上免费的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); 
} 

不过,我的想法玩弄如果自由绘制的一切,但只有当我想不通我的一些作为其他问题:

  • 整个布局重新定位的FrameLayout正确
  • 正确调整大小的FrameLayout
  • 属性动画

这就是我得到的。