Android布局:游戏板

时间:2014-02-01 02:53:04

标签: android android-layout layout

我看过很多关于游戏板的问题,但似乎没有一个问题涉及我的具体问题;虽然我认为这是一个很常见的。

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

我的目标是冰淇淋三明治及以上,

我尝试的第一个布局是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>

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

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

所以现在我已经没有了想法,并且会感激任何建议。

1 个答案:

答案 0 :(得分:1)

所以我没有找到一个在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时,高度总是会拉伸以填充屏幕上的剩余空间;这意味着我无法将董事会集中在一起。

为了解决这个问题,我删除了GridLayout的weight属性,并将其包装在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();

然后我有一个自动调整,自动居中游戏板! :)

相关问题