简体   繁体   English

Android GridView无法正确缩放

[英]Android GridView Not Scaling Properly

I have spent hours on this and looked at every related SO question I could find without a solution. 我花了几个小时在上面,研究了没有解决方案就可以找到的每个相关的SO问题。

Here is my problem: 这是我的问题:

I have a gridview of images. 我有图像的gridview。 I need 3 columns (to match the iOS version and aesthetics). 我需要3列(以匹配iOS版本和美观性)。 If I set the numColumns to 3, I get a lot extra space between the top and bottom of the image row for each row. 如果将numColumns设置为3,则每一行在图像行的顶部和底部之间会得到很多额外的空间。 If I set the width to autofit, I always get 2, they look better but would prefer 3 columns. 如果将宽度设置为自动调整,我总是得到2,它们看起来更好,但希望使用3列。

What am I missing? 我想念什么?

Layout: 布局:

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

屏幕截图

I am hoping it is something easy that I am just missing. 我希望我只是想念这件事很容易。 Thanks in advance. 提前致谢。

UPDATED: Added Screenshot. 更新:添加了屏幕截图。 The problem is that only 1 row is showing, if you scroll down, you see the other 3 rows, but there is a huge space in between. 问题是只显示1行,如果向下滚动,则会看到其他3行,但是它们之间有很大的空间。

<edit> <编辑>

Please find "Tip 3" below. 请在下面找到“提示3”。 Example with code can be found here . 带有代码的示例可以在这里找到。

</edit> </ edit>

I think the problem is that you set the layout_height of your <GridView> to wrap_content . 我认为问题在于您将<GridView>layout_height设置为wrap_content The grid view doesn't seem to be able to calculate it's total, wrapped height properly, hence, it's showing one row only. 网格视图似乎无法正确计算总高度,换行高度,因此,它仅显示一行。

You could either set the layout_height to match_parent (or any other, fixed height - like 120dp or whatever) or your could try to extend the GridView Java object and do some own calculation (you would then need to use your custom grid view object in your XML-layout as well: <com.package.to.MyCustomGridView> ). 您可以将layout_height设置为match_parent (或任何其他固定高度-如120dp或其他值),或者可以尝试扩展GridView Java对象并进行一些自己的计算(然后,您需要在您的自定义网格视图对象中使用以及XML布局: <com.package.to.MyCustomGridView> )。

I do need to stress, though, that there aren't any well defined way of getting hold of the number of columns from the grid view in Java code prior to API 11 (you would want this in order to calculate how many rows your data adapter would produce). 不过,我确实要强调一点,在API 11之前,没有任何明确定义的方法可以从Java代码中的网格视图中获取列数(您希望这样做是为了计算数据的行数)适配器会产生)。 The getNumColumns was introduced in API 11. Neither is there a proper way of finding the spacing between rows (the getHorizontalSpacing() and getVerticalSpacing() methods were introduced in API 16). API 11中引入了getNumColumns 。没有找到行之间间距的正确方法getVerticalSpacing() API 16中引入了getHorizontalSpacing()getVerticalSpacing()方法)。

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

Tip 1: I haven't tested this my self, but you could try something like this: 提示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>

Ie including your grid view in the linear layout and adding all available space to it after the image view has been laid out. 即在线性布局中包括网格视图,并在布局图像视图后向其中添加所有可用空间。

Tip 2: You could write your own list view. 提示2:您可以编写自己的列表视图。 There seems to be some good candy on the Sony Mobile tutorial site (and no, I'm not employed by Sony Mobile :-), nevertheless; 不过,索尼移动教程网站上似乎有很多不错的东西(不,我没有受索尼移动公司雇用:-); a good tutorial is a good tutorial): http://developer.sonymobile.com/2010/05/20/android-tutorial-making-your-own-3d-list-part-1/ 好教程就是好教程): http : //developer.sonymobile.com/2010/05/20/android-tutorial-making-your-own-3d-list-part-1/

Tip 3: You could extend the Java GridView object and override the onMeasure method as below example. 提示3:您可以扩展Java GridView对象并重写onMeasure方法,如下例所示。 Remember to refer to your extending GridView class in the Android layout XML file (like <com.package.MyGridView android:layout_width="match_parent" android:layout_height="wrap_content" ... /> ) 记住要在Android布局XML文件中引用扩展的GridView类(例如<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);
}

Hopefully you'll find some inspiration in this :-) 希望您会在此找到一些启发:-)

Cheers, -- dbm 干杯-dbm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM