如何设置GridView的细胞基于屏幕高度的高度? [英] How can I set the height of GridView cell based on screen height?

查看:121
本文介绍了如何设置GridView的细胞基于屏幕高度的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的布局,我只有八个单元。
我想要的细胞分裂的所有可用屏幕上的空间。这可能吗?

这是我的GridView布局:

 < GridView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / dataGrid的
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:填充=10px的
    机器人:verticalSpacing =10px的
    机器人:horizo​​ntalSpacing =10px的
    机器人:为numColumns =2
    机器人:重力=补/>
 

这是该项目的布局:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:填充=6dip>
  <的TableRow
      机器人:layout_width =FILL_PARENT
      机器人:layout_height =FILL_PARENT>
    <的LinearLayout
        机器人:方向=垂直
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>
      <的TextView
          机器人:layout_width =FILL_PARENT
          机器人:layout_height =FILL_PARENT
          机器人:文字颜色=#E00000
          机器人:TEXTSTYLE =黑体
          机器人:重力=左
          机器人:TEXTSIZE =16dip/>
      <的TextView
          机器人:layout_width =FILL_PARENT
          机器人:layout_height =FILL_PARENT
          机器人:文字颜色=#000000
          机器人:TEXTSTYLE =正常
          机器人:重力=右
          机器人:TEXTSIZE =12dip/>
    < / LinearLayout中>
  < /的TableRow>
< / LinearLayout中>
 

解决方案

不幸的是,的GridView 是一个棘手的布局来操作,以获得内部的意见,以适应像你这样一个特定的大小尝试。我认为这是比较合适的思考 GridView控件的作为一种先进的的ListView 为显示变长,滚动列表像缩略图在一个相册,而不是一个网格。我开始走上这条路时,我本来想做一个固定大小的格子的游戏,但它真的不是一个合适的布局都没有。

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

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    机器人:weightSum =1.0>

    <的LinearLayout
        机器人:layout_width =0dp
        机器人:layout_height =0dp
        机器人:layout_weight =0.5
        机器人:方向=横向
        机器人:weightSum =1.0>

        <! - 下降4项这里的0.25 layout_weight  - >

    < / LinearLayout中>
    <的LinearLayout
        机器人:layout_width =0dp
        机器人:layout_height =0dp
        机器人:layout_weight =0.5
        机器人:方向=横向
        机器人:weightSum =1.0>

        <! - 下降4项这里的0.25布局重 - >

    < / LinearLayout中>
< / LinearLayout中>
 

这将使一个固定的2x4的网格,总是完全充满屏幕。

In my layout I have only eight cells.
I want the cell divide all the space available on the screen. Is this possible?

This is my gridview layout:

<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" />

And this is the item layout:

<?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>

解决方案

Unfortunately, GridView is a tricky layout to manipulate to get internal views to fit a specific size like you are attempting. I think it is more appropriate to think of GridView as an advanced ListView for showing variable-length, scrollable lists like thumbnails in a photo album, as opposed to a "grid." I started down this route when I originally wanted to make a fixed sized grid for a game, but it is really not an appropriate layout for that at all.

Since your grid is always 8 cells, might I suggest using nested LinearLayouts (or if you don't want all the cells exactly the same size, the more complex TableLayout / TableRow combo) instead:

<?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 >

This would make a fixed 2x4 grid that always exactly fills the screen.

这篇关于如何设置GridView的细胞基于屏幕高度的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆