GridView and excess space padding

浪子不回头ぞ 提交于 2019-11-26 16:55:05

问题


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

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.

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


回答1:


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>

The most important thing is the clipChildren property.



来源:https://stackoverflow.com/questions/7705330/gridview-and-excess-space-padding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!