2012-09-11 68 views
0

我花了好几个小时的时间,看了没有解决方案时找到的每个相关的SO问题。Android GridView不能正确缩放

这里是我的问题:

我有图像的GridView控件。我需要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> 

Screenshot

我希望它是简单的东西,我只是缺少。提前致谢。

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

+2

参考这个答案http://stackoverflow.com/questions/12363106/gridview-height-is-too-tall-when-using-an-imageview-inside/12363218#12363218 –

+0

这并没有帮助。已更新以显示更多的XML – RiddlerDev

+0

您可以发布截图或想要的结果样机吗?你现在有什么。我刚刚使用了三列的gridView,没有任何问题 – Jin35

回答

3

<编辑>

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

< /编辑>

我认为这个问题是您设置<GridView>wrap_contentlayout_height。网格视图似乎无法正确计算它的总高度,因此它只显示一行。

您既可以设置layout_heightmatch_parent(或任何其他固定高度 - 像120dp或其他)或您可以尽量延长GridView Java对象,做一些自己的计算(那么你就需要使用您的自定义网格查看您的XML布局中的对象以及:<com.package.to.MyCustomGridView>)。

但是,我需要强调的是,在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方法如下面的例子。请记住,请参阅您的扩展GridView类Android的布局XML文件中(如<com.package.MyGridView android:layout_width="match_parent" android:layout_height="wrap_content" ... />

@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); 
} 

希望你能找到一些灵感在这个:-)

干杯, - DBM

+0

谢谢,我今晚会试试这个,告诉你它是怎么回事 – RiddlerDev

+0

仍然不完美,但是提示3让我走上了正确的道路,只需要现在就使用覆盖。谢谢! – RiddlerDev

+0

太棒了!乐意提供帮助:-) – dbm