有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在屏幕上分布一个数据网格,它的所有项目都是可见的

我正在编写我的第一个安卓应用程序。我使用的是安卓2.3.3SDK。 该游戏是一种纸牌游戏,屏幕上有4x4格的纸牌

当游戏开始时,屏幕上唯一可见的就是数据网格(没有菜单或什么都没有)

因此,游戏活动布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/faces_grid_view"
    安卓:numColumns="4"
    安卓:verticalSpacing="1dp"
    安卓:horizontalSpacing="1dp"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
>
</GridView>

这是GameActivity类的onCreate函数的相关代码:

          Display display = getWindowManager().getDefaultDisplay();
            final int width  = display.getWidth();
            final int height = display.getHeight();

            final int bottomPadding=150;
             //Display display = getWindowManager().getDefaultDisplay();
            gameTurns = new ArrayList<GameTurn>();
            imageWidth=width/4;
            imageHeight=(height-bottomPadding)/4;
            imageAdapter = new ImageAdapter(this,imageWidth,imageHeight);
            facesGridView = (GridView) findViewById(R.id.faces_grid_view);
            facesGridView.setAdapter(imageAdapter);

在onCreate()代码中,我得到的是defaultDisplaySize,将其除以4,这样我就知道图像可以拍摄的最大高度和宽度

  • 出于某种原因,我需要添加一个底部填充。我在galaxy note和galaxy tab 10.1上查看了它,也许getDefaultDisplay也会返回平板电脑底部工具栏的空间,这样图片就可以在没有任何填充的情况下覆盖整个屏幕

如您所见,我为GridView设置了一个适配器。适配器扩展了BaseAdapter 我把我之前计算的宽度和高度粘贴到他的构造器上

适配器的getView函数包含以下内容:

      public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                LayoutParams param = new    LinearLayout.LayoutParams(this.imageWidth,this.imageHeight);
                imageView.setLayoutParams(new GridView.LayoutParams(param));
                imageView.setAdjustViewBounds(true);
                imageView.setScaleType(ScaleType.CENTER_INSIDE);
                imageView.setPadding(1,1,1,1);
            } else {
                imageView = (ImageView) convertView;
            }

有几个问题:

  1. 是否有一种方法可以将datagrid配置为在所有屏幕上都可见,并且在不配置图像宽度或高度的情况下不滚动显示所有项目?为什么我需要为这么简单的任务如此努力
  2. 如果我真的需要自己计算东西,有没有办法得到实际的活动高度和宽度,这样我就不需要添加任何填充?welp我希望它能在风景画和肖像画中适用于所有大小的屏幕

我已经厌倦了改变和玩配置很多天了,这是我迄今为止所取得的最好成绩,因此任何关于这个问题的信息都将不胜感激

更新

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent"
    安卓:stretchColumns="*"
    安卓:weightSum="4" >

<TableRow
    安卓:id="@+id/tableRow1"
    安卓:layout_width="wrap_content"
    安卓:layout_height="0dp"
    安卓:layout_weight="1"
     >
<ImageView
    安卓:id="@+id/imageView1"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />
<ImageView
    安卓:id="@+id/imageView2"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />
<ImageView
    安卓:id="@+id/imageView3"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />
<ImageView
    安卓:id="@+id/imageView4"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />

</TableRow>
<TableRow
    安卓:id="@+id/tableRow2"
    安卓:layout_width="wrap_content"
    安卓:layout_height="0dp"
    安卓:layout_weight="1"
     >
<ImageView
    安卓:id="@+id/imageView5"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />
<ImageView
    安卓:id="@+id/imageView6"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />
<ImageView
    安卓:id="@+id/imageView7"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />
<ImageView
    安卓:id="@+id/imageView8"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />

</TableRow>
    <TableRow
    安卓:id="@+id/tableRow3"
    安卓:layout_width="wrap_content"
    安卓:layout_height="0dp"
    安卓:layout_weight="1"
    >
<ImageView
    安卓:id="@+id/imageView9"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />
<ImageView
    安卓:id="@+id/imageView10"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />
<ImageView
    安卓:id="@+id/imageView11"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />
<ImageView
    安卓:id="@+id/imageView12"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />

</TableRow>
    <TableRow
    安卓:id="@+id/tableRow4"
    安卓:layout_width="wrap_content"
    安卓:layout_height="0dp"
    安卓:layout_weight="1"
    >
<ImageView
    安卓:id="@+id/imageView13"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />
<ImageView
    安卓:id="@+id/imageView14"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />
<ImageView 
    安卓:id="@+id/imageView15"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />
<ImageView
    安卓:id="@+id/imageView16"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:contentDescription="@string/face"
    安卓:onClick="onItemClick"
     />

</TableRow>

</TableLayout>

此xml文件将生成4个图像,这些图像居中并垂直分布(这适用于所有屏幕,因此下面可能会有更多图像)

screenshot

我错过了什么?:)

谢谢

Kfir


共 (1) 个答案

  1. # 1 楼答案

    is there a way just to configure the datagrid to be visible on the all screen and to show all items with no scrolling without configuration the image width or height ? why do I need to work so hard for such a simple task?

    这是一个简单的任务,因为您使用了错误的小部件。如果单元格的网格比屏幕大,您通常会使用GridView,这样您就可以从GridView的回收机制中获益(以提高性能并避免应用程序内存出现问题)。这不是您的情况,因为您希望所有单元格都可见

    我将使用一个宽度和高度设置为FILL_PARENTTableLayout(将stretchColumns设置为*,这样行列的宽度将相同),它将包含四个TableRows(将layout_weight设置为1(将weighSum设置为4,这样它们的高度将相等),另外TableRowlayout_height将设置为0dp。每个TableRow将包含四个ImageViews

    if I do need to calculate stuff myself, is there a way to get the actual Activity height and width so I won't need to add any padding? Help I want it to work on all scree sizes in landscape and portrait.

    我建议您使用我在上面发布的解决方案。如果您仍然想使用GridView,那么让它填充所有的宽度/高度(通过使用fill_parent而不是像对layout_width/height那样使用wrap_content),并在onCreate方法中使用Handlerpost一个Runnable来检索GridView的{}并使用这些设置适配器