我已经看到了很多有关游戏板的问题,但是似乎没有一个问题可以解决我的特定问题。尽管我认为这很普遍。

我想使用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>


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

我看到的一个建议是手动指定元素的大小-这是可行的,但这将意味着为不同的屏幕大小指定不同的值,并且每次更改其余布局时都需要更改-我理想上希望能够采用一个解决方案并在多个项目中使用它。

所以,现在我用尽了所有的想法,希望您能提出任何建议。

最佳答案

因此,我没有找到在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();


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

07-26 09:39