【问题标题】:Android Layout: Game BoardAndroid 布局:游戏板
【发布时间】:2014-02-02 10:58:32
【问题描述】:

我看过很多关于游戏板的问题,但似乎没有一个能涵盖我的具体问题;虽然我想这是一个很常见的。

我想设计一个游戏板,使用 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>

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

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

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

【问题讨论】:

    标签: android android-layout layout


    【解决方案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();
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 2012-03-13
      相关资源
      最近更新 更多