【问题标题】:Gridview return only one rowGridview 只返回一行
【发布时间】:2014-11-08 14:02:23
【问题描述】:

我在 相对布局 中使用 GridView。我希望 GridView 显示 尽可能高以显示所有行。但是,我只看到 设置时为一行(layout_width=MATCH_PARENT 和) layout_height=WRAP_CONTENT。如果我将 layout_height 设置为 等于多行,然后我确实看到了这些行。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>

<RelativeLayout
    android:id="@+id/layout_month"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >

    <TextView
        android:id="@+id/textv_month_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:gravity="center_horizontal|center_horizontal"
        android:textColor="#ffffff"
        android:textSize="7dp" />

    <ImageView
        android:id="@+id/top_bar"
        android:layout_width="fill_parent"
        android:layout_height="15dp"
        android:layout_below="@+id/textv_month_name"
        android:background="@drawable/calendar_top_small"
        android:clickable="false" />

    <GridView
        android:id="@+id/gridv_inner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_bar"
        android:layout_gravity="center"
        android:clickable="false"
        android:gravity="center"
        android:numColumns="7"
        android:stretchMode="columnWidth"
         >
    </GridView>

    <Button
        android:id="@+id/monthBtn"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent" />
</RelativeLayout>

</RelativeLayout>

任何帮助都会很重要

【问题讨论】:

标签: android datagridview


【解决方案1】:

这是一个非常有用的答案。

GridView 不喜欢它的高度的 wrap_content 当它被放置在一个垂直狭窄的空间中时;它只显示了单行项目(滚动该单行中的所有内容)。

我的修复的简短版本包括对 GridView 进行子类化并覆盖 onMeasure 方法。修复的本质在于将测量的高度规格设置为一个大得离谱的数字。

MyGridview.Java

   package com.arun.calendar;

   import android.content.Context;
   import android.util.AttributeSet;
   import android.widget.GridView;

   public class MyGridView extends GridView {

 public MyGridView(Context context) {
        super(context);
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

        if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {

            // The two leftmost bits in the height measure spec have
            // a special meaning, hence we can't use them to describe height.
            heightSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >>2, MeasureSpec.AT_MOST);
        }
        else {
            // Any other height should be respected as is.
            heightSpec = heightMeasureSpec;
        }

        super.onMeasure(widthMeasureSpec, heightSpec);
    }
}

还有新的 XML 文件

   <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

<RelativeLayout
    android:id="@+id/layout_month"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textv_month_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:gravity="center_horizontal|center_horizontal"
        android:textColor="#ffffff"
        android:textSize="7dp" />

    <ImageView
        android:id="@+id/top_bar"
        android:layout_width="fill_parent"
        android:layout_height="15dp"
        android:layout_below="@+id/textv_month_name"
        android:background="@drawable/calendar_top_small"
        android:clickable="false" />

    <com.arun.calendar.MyGridView
        android:id="@+id/gridv_inner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_bar"
        android:layout_gravity="center"
        android:clickable="false"
        android:gravity="center"
        android:numColumns="7"
        android:stretchMode="columnWidth" />
</RelativeLayout>

<Button
    android:id="@+id/monthBtn"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent" />

</RelativeLayout>

在java文件中

 MyGridView  viewHolder.gridView = (MyGridView) v.findViewById(R.id.gridv_inner);

【讨论】:

  • 大声笑@“这是一个非常有用的答案”
  • 你是救生员! :)
  • 优秀的答案!谢谢!
猜你喜欢
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
相关资源
最近更新 更多