2014-03-28 99 views
6

我正在使用GridView相对布局。我希望GridView显示为 ,并根据需要显示所有行。但是,设置时我只看到 一行(layout_width = MATCH_PARENT和) layout_height = WRAP_CONTENT。如果我将layout_height设置为 等于多行的大小,那么我确实会看到这些行。Gridview只返回一行

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

任何帮助将很可观

+0

发表您的XML文件和代码也。 – Piyush

+0

https://groups.google.com/forum/#!topic/android-developers/P5zlnvpbQLU –

回答

39

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

当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); 
+4

LOL @“这是一个非常有用的答案” –

+0

你是一个拯救生命的人! :) – Jas

+0

Upvote !!!你救了人的生命 –