如何根据屏幕高度来设置GridView单元格的高度?

问题描述:

在我的布局中,我只有八个单元格。
我想要的细胞分裂的所有屏幕上的可用空间。这可能吗?如何根据屏幕高度来设置GridView单元格的高度?

这是我的GridView布局:

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/dataGrid" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10px" 
    android:verticalSpacing="10px" 
    android:horizontalSpacing="10px" 
    android:numColumns="2" 
    android:gravity="fill" /> 

这是该项目的布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="6dip" > 
    <TableRow 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:textColor="#E00000" 
      android:textStyle="bold" 
      android:gravity="left" 
      android:textSize="16dip"/>  
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:textColor="#000000" 
      android:textStyle="normal" 
      android:gravity="right" 
      android:textSize="12dip"/> 
    </LinearLayout> 
    </TableRow> 
</LinearLayout> 

首先,使用PS代替PX。这将允许它扩展到不同的DPI设备。

其次,让每个单元是大小均匀,你可以把一个在的tableView包含清晰的图像的每个细胞。用PS(不是px)设置图像的大小。表视图应该允许您在每个单元格中对布局对象进行分层,并且清晰的图形应该保留所有单元格的大小相同。

我还要去除任何填充。

让我们知道。

+0

请教我,但我在三天内没有网络连接...无论如何,我希望gridview能够在屏幕上划分所有的空间,基于任何android设备,并且这个解决方案并不擅长;谢谢。问候 – 2011-03-01 13:50:23

你应该尝试[机器人:stretchMode] [1]

[1]:http://developer.android.com/reference/android/widget/GridView.html#attr_android:stretchMode:定义列应如何拉伸以填充可用的空空间,如果有的话。

不幸的是,GridView是一个棘手的布局操作以获得内部的意见就像你正试图以适应特定的大​​小。我认为将GridView设想为高级ListView更适合于显示可变长度的可滚动列表,如相册中的缩略图,而不是“网格”。当我最初想为游戏制作一个固定大小的网格时,我开始走下这条路线,但它根本不是一个合适的布局。

由于网格始终是8个细胞,可能我建议使用嵌套LinearLayout秒(或者,如果你不希望所有的细胞大小完全相同,更复杂的TableLayout/TableRow组合)代替:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:weightSum="1.0" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="0.5" 
     android:orientation="horizontal" 
     android:weightSum="1.0" > 

     <!-- Drop in 4 items here with layout_weight of 0.25 --> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="0.5" 
     android:orientation="horizontal" 
     android:weightSum="1.0" > 

     <!-- Drop in 4 items here with layout weight of 0.25 --> 

    </LinearLayout > 
</LinearLayout > 

这将使固定2x4的网格,总是精确地填满屏幕。