安卓布局 : Game Board

标签 android android-layout layout

我看到了很多关于游戏板的问题,但似乎没有一个能涵盖我的具体问题;尽管我认为这是很常见的。

我想设计一个游戏板,使用 Android 布局。布局将有 X 行和 Y 列。它应该扩展以充分利用可用空间,但所有元素都应该是方形的。

我的目标是 Ice Cream Sandwich 及以上,

我尝试的第一个布局是 GirdLayout - 但很快就放弃了,因为无法分配未使用的空间。

其次,我尝试了嵌套的 LinearLayouts。例如..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".GamePlay" >


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

    <Button
        android:text="0"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>


    <Button
        android:text="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:text="2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:text="3"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:text="4"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

</LinearLayout>

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

    <Button
        android:text="0"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>


    <Button
        android:text="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:text="2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:text="3"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:text="4"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

</LinearLayout>

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

    <Button
        android:text="0"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>


    <Button
        android:text="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:text="2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:text="3"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:text="4"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    </LinearLayout>
</LinearLayout>

但是,据我所知,无法对子对象的宽度和高度进行所需级别的控制。当行数和列数匹配时,它几乎在纵向模式下产生可接受的结果,但在横向模式下板被拉伸(stretch) - 显然这将取决于布局中的其他元素

我看到的一个建议是手动指定元素的大小 - 这可行,但这意味着为不同的屏幕尺寸指定不同的值,并且每次布局的其余部分发生变化时都需要更改 - 我理想情况下,希望能够采用单一解决方案并将其用于多个项目。

所以现在我已经用完了 ides,如果有任何建议,我将不胜感激。

最佳答案

所以我还没有找到在 xml 中管理它的解决方案,但是可以使用 ViewTreeObserver.OnGlobalLayoutListener 在代码中执行此操作

我使用 GridLayout 添加控件,其 xml 最初如下所示:

    <GridLayout
        android:background="#FFFF00"
     android:id="@+id/gridGameBoard"
     android:rowCount="3"
     android:columnCount="3"
     android:layout_width="fill_parent"
     android:layout_height="0dp"
     android:layout_margin="0dp"
     android:layout weight="1"
     android:padding="0dp" />

然后我将以下几行添加到我的 onCreate 中:

this.oGameBoard = (GridLayout) this.findViewById(R.id.gridGameBoard);
this.oGameBoard.getViewTreeObserver().addOnGlobalLayoutListener(this.SquareIfy());

和函数 SquareIfy() 如下:

ViewTreeObserver.OnGlobalLayoutListener SquareIfy()
{
    return new ViewTreeObserver.OnGlobalLayoutListener() 
    {

        @Override
        public void onGlobalLayout() 
        {
            int width = MyActivity.this.oGameBoard.getMeasuredWidth();      
            int height = MyActivity.this.oGameBoard.getMeasuredHeight();
            int cols = MyActivity.this.oGameBoard.getColumnCount();
            int rows = MyActivity.this.oGameBoard.getRowCount();
            int tileCount = cols*rows;

            double sizeA = (width/cols);
            double sizeB = (height/rows);

            double smallestSize = (sizeA<sizeB?sizeA:sizeB);
            int smallestSizeInt = (int) Math.floor(smallestSize);

            for(int x=0;x<=tileCount-1;x++)
            {
                try
                {
                    Button b = new Button(MyActivity.this);
                    b.setText(String.valueOf(x));
                    b.setPadding(0, 0, 0, 0);

                    GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
                    lp.width = smallestSizeInt;
                    lp.height = smallestSizeInt;
                    lp.leftMargin=0;
                    lp.rightMargin=0;
                    lp.topMargin=0;
                    lp.bottomMargin=0;
                    b.setLayoutParams(lp);

                    MyActivity.this.oGameBoard.addView(b);
                    MyActivity.this.oGameBoard.getLayoutParams().width=smallestSizeInt * cols;
                    MyActivity.this.oGameBoard.getLayoutParams().height=smallestSizeInt * rows;
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
            }

            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) 
            {
                MyActivity.this.oGameBoard.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } 
            else 
            {
                MyActivity.this.oGameBoard.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }



        }
    };
}

这在一定程度上奏效了,但由于 GridLayout 的权重为 1,因此高度总是会拉伸(stretch)以填充屏幕上的剩余空间;这意味着我无法将棋盘居中。

为了解决这个问题,我删除了 GridLayout 的权重属性,并将其包装在 LinearLayout 中:

<LinearLayout
    android:id="@+id/shellGameBoard"
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <GridLayout
        android:background="#FFFF00"
     android:id="@+id/gridGameBoard"
     android:rowCount="3"
     android:columnCount="3"
     android:layout_width="fill_parent"
     android:layout_height="0dp"
     android:layout_margin="0dp"
     android:padding="0dp" />

</LinearLayout>

然后我将 OnCreate 代码更改为:

    this.oGameBoardShell = (LinearLayout) this.findViewById(R.id.shellGameBoard);

    this.oGameBoard = (GridLayout) this.findViewById(R.id.gridGameBoard);
    this.oGameBoard.getViewTreeObserver().addOnGlobalLayoutListener(this.SquareIfy());

在 SquarIfy() 中我改变了:

int width = MyActivity.this.oGameBoard.getMeasuredWidth();      
int height = MyActivity.this.oGameBoard.getMeasuredHeight();

int width = GamePlay.this.oGameBoardShell.getMeasuredWidth();       
int height = GamePlay.this.oGameBoardShell.getMeasuredHeight();

有了它,我就有了一个自动调整大小、自动居中的游戏板! :)

关于安卓布局 : Game Board,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21493576/

相关文章:

android - 将数据写入本地 JSON 文件

View 上的Android动态气泡

android - 在android Activity 的右上角显示对话框

jquery - 水平滚动布局左/右: jQuery/CSS quiz

java - 我可以声明android :id as array in Android?吗

html - 第一层水平布局的三级 CSS 菜单

android - 在小部件配置 Activity 中设置 RESULT_CANCELED 是否多余?

android Mediaplayer 无法在模拟器中工作

java - 使 getLatLng 正常工作,但无法从响应中删除 ( )

android - 滚动时带阴影效果的透明工具栏