【问题标题】:How can I set the height of GridView cell based on screen height?如何根据屏幕高度设置 GridView 单元格的高度?
【发布时间】:2011-02-25 17:19:37
【问题描述】:

在我的布局中,我只有八个单元格。
我希望单元格划分屏幕上的所有可用空间。这可能吗?

这是我的 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>

【问题讨论】:

    标签: android layout gridview


    【解决方案1】:

    不幸的是,GridView 是一个棘手的布局,难以操作以使内部视图适合您正在尝试的特定大小。我认为将GridView 视为高级ListView 更合适,用于显示可变长度、可滚动的列表,如相册中的缩略图,而不是“网格”。当我最初想为游戏制作一个固定大小的网格时,我就开始了这条路线,但这根本不适合那种布局。

    由于您的网格始终是 8 个单元格,我是否建议使用嵌套的 LinearLayouts(或者如果您不希望所有单元格的大小完全相同,则使用更复杂的 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 网格。

    【讨论】:

      【解决方案2】:

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

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

      我还会删除任何填充。

      让我们知道。

      【讨论】:

      • 打扰一下,但我大约三天没有互联网连接......无论如何,我希望 gridview 根据任何 android 设备划分屏幕上的所有空间,这个解决方案不好一点也不;谢谢。问候
      【解决方案3】:

      你应该试试 [android:stretchMode][1]

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

      【讨论】:

        猜你喜欢
        • 2014-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多