【问题标题】:Android GridView Not Scaling ProperlyAndroid GridView 无法正确缩放
【发布时间】:2012-11-20 03:55:14
【问题描述】:

我已经为此花费了数小时,并查看了我可以找到的所有相关 SO 问题,但没有解决方案。

这是我的问题:

我有一个图像网格视图。我需要 3 列(以匹配 iOS 版本和美学)。如果我将 numColumns 设置为 3,我会在每一行的图像行的顶部和底部之间获得很多额外的空间。如果我将宽度设置为自动调整,我总是得到 2,它们看起来更好,但更喜欢 3 列。

我错过了什么?

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top" >

<LinearLayout 
    android:id="@+id/llayout"       
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    >
    <ImageView
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#666666"
        android:clickable="false"
        android:contentDescription="@string/title_card_collection"
        android:src="@drawable/sportscard2x" />
</LinearLayout>

<GridView
    android:id="@+id/cardGrid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_above="@+id/llayout"
    android:gravity="center"
    android:horizontalSpacing="2dip"
    android:numColumns="3"
    android:stretchMode="columnWidth"
    android:layout_alignParentBottom="true"
    android:verticalSpacing="2dip" >
</GridView>

我希望我只是想念一些简单的事情。提前致谢。

更新:添加截图。问题是只显示了 1 行,如果向下滚动,您会看到其他 3 行,但中间有很大的空间。

【问题讨论】:

  • 这根本没有帮助。更新以显示更多 XML
  • 你能张贴你想要的结果的截图或模型吗?你现在有什么。我刚刚用了三列的gridView,没有任何问题
  • 我检查了您的布局,它按照您的期望工作正常。
  • 添加了图片。希望这更清楚。我需要它来显示所有行。 (类似于图库)

标签: android gridview


【解决方案1】:

请在下面找到“提示 3”。代码示例可以在here找到。

我认为问题在于您将&lt;GridView&gt;layout_height 设置为wrap_content。网格视图似乎无法正确计算其总包裹高度,因此,它仅显示一行。

您可以将 layout_height 设置为 match_parent(或任何其他固定高度 - 例如 120dp 或其他),或者您可以尝试扩展 GridView Java 对象并进行一些自己的计算(您可以然后需要在您的 XML 布局中使用您的自定义网格视图对象:&lt;com.package.to.MyCustomGridView&gt;)。

不过,我确实需要强调,在 API 11 之前的 Java 代码中,没有任何明确定义的方法可以从网格视图中获取列数(您可能需要这样来计算有多少您的数据适配器将产生的行)。 getNumColumns 是在 API 11 中引入的。也没有找到行间距的正确方法(API 16 中引入了 getHorizontalSpacing()getVerticalSpacing() 方法)。

供您参考:http://developer.android.com/reference/android/widget/GridView.html

提示 1: 我自己没有测试过,但你可以试试这样的:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...
        />

    <GridView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        ...
        />

</LinearLayout>

即在线性布局中包括您的网格视图,并在布局图像视图后为其添加所有可用空间。

提示 2: 您可以编写自己的列表视图。索尼移动教程网站上似乎有一些不错的糖果(不,我没有受雇于索尼移动:-),不过;好教程就是好教程):http://developer.sonymobile.com/2010/05/20/android-tutorial-making-your-own-3d-list-part-1/

提示 3: 您可以扩展 Java GridView 对象并覆盖 onMeasure 方法,如下例所示。请记住在 Android 布局 XML 文件中引用您扩展的 GridView 类(如 &lt;com.package.MyGridView android:layout_width="match_parent" android:layout_height="wrap_content" ... /&gt;

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightSpec;

    // The great Android "hackatlon" (in order to enable "wrap_content" on grid views).
    if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
        heightSpec = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    }
    else {
        heightSpec = heightMeasureSpec;
    }

    super.onMeasure(widthMeasureSpec, heightSpec);
}

希望你能从中找到一些灵感:-)

干杯, -- 数据库

【讨论】:

  • 谢谢,今晚我会试一试,告诉你进展如何
  • 仍然不完美,但提示 3 让我走上了正确的道路,现在只需要使用覆盖即可。谢谢!
  • 嗯,还有差距。无法摆脱它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 2019-05-18
  • 2018-05-19
  • 2012-12-16
  • 2013-08-21
相关资源
最近更新 更多