GridView 和多余的空间填充 [英] GridView and excess space padding

查看:27
本文介绍了GridView 和多余的空间填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 上的网格视图布局有问题.我找不到消除网格视图中多余空间的解决方案.我尝试了很多东西(numColumns、columnWidth、stretchMode、gravity)和建议(来自 StackOverflow),但没有一个能正常工作.我花了将近 8 个小时来解决这个问题.这是网格视图的代码:

I have a problem with grid view layout on Android. I can't find solution to eliminate extra space in grid view. I tried a lot of things (numColumns, columnWidth, stretchMode, gravity) and advices (from StackOverflow), but nothing works correctly. I spent almost 8 hours with this problem. Here is a code of grid view:

<GridView
        android:id="@+id/lookbook_gridview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:listSelector="@null"
        android:padding="0dip"
        android:layout_margin="0dip"
        android:verticalSpacing="0px"
        android:horizontalSpacing="0px"

        android:numColumns="auto_fit"
        android:columnWidth="160px"
        android:stretchMode="columnWidth"
        android:gravity="center" 
        android:layout_gravity="center"

        android:background="#000"
        android:cacheColorHint="#000"
        android:descendantFocusability="afterDescendants"
        android:layout_alignParentTop="true"
        android:layout_above="@id/buttons">
    </GridView> 

我还尝试以编程方式减少额外空间:

I also tried to reduce extra space programically:

private void setGridview()
{  
    GridView gridview = (GridView) findViewById(R.id.lookbook_gridview);
    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

    int gridSize = display.getWidth();
    int count = gridSize / 160; // image has 160x160 px
    int colWidth = (gridSize / count) - PADDING;

    gridview.setColumnWidth(colWidth);
    gridview.setNumColumns(count);
}

但它仅适用于我的 HTC Desire(右),但适用于具有相同显示分辨率和相同 API 版本的模拟器(左) - 它不起作用.

But it works only on my HTC Desire (right), but on emulator (left) with the same display resolution and the same API version - it is not working.

有人知道如何在没有任何特殊填充或空间的情况下在 gridview 中设置图像以在所有分辨率和设备上成功工作吗?

Does somebody know, how to set images in gridview without any special padding or space to work successfully with all resolutions and devices?

推荐答案

为了解决我的问题,我使用了这个 gridView:

To solve my problem I used this gridView:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyGrid" android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:padding="0dp"
    android:verticalSpacing="2dp" 
    android:horizontalSpacing="2dp"
    android:scrollingCache="true" 
    android:smoothScrollbar="true"
    android:clipChildren="true" 
    android:alwaysDrawnWithCache="true"
    android:numColumns="auto_fit" 
    android:columnWidth="100dp"
    android:stretchMode="columnWidth" 
    android:gravity="center_horizontal"
    android:background="#000000">
</GridView>

最重要的是 clipChildren 属性.

这篇关于GridView 和多余的空间填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆