Android: Reduce space between columns in GridView

前端 未结 7 1958
情深已故
情深已故 2021-02-04 05:14

See attached screenshot where I am attempting to reduce the space between the columns in my GridView.

My main.xml is as follows:



        
相关标签:
7条回答
  • 2021-02-04 05:26

    Hi i had the same problem i solved by mygridView.setStretchMode(GridView.NO_STRETCH);

    0 讨论(0)
  • 2021-02-04 05:32

    I had the same problem. I minimized column space by doing this

    for gridView

    android:numColumns="2"
    android:columnWidth="90dp"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="-30dp"
    android:stretchMode="columnWidth"
    

    for imageView

    android:layout_width="115dp"
    android:layout_height="150dp"
    android:padding="1dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="-15dp"
    
    0 讨论(0)
  • 2021-02-04 05:36

    Remove Gravity attribute and use the following mode

    android:stretchMode="NONE"
    
    0 讨论(0)
  • 2021-02-04 05:36

    I was having similar issue of having slight horizontal spacing between the columns in few devices. ( I did not required any space. ) I was properly calculating the grid item width by dividing it with total width. I was getting this issue only on Lenovo devices.

    following code was in activity related to grid.

    int myRequestedColumnWidth = myGridViewContentMain.getRequestedColumnWidth();
    
    Point size = new Point();
    getWindowManager().getDefaultDisplay().getSize( size );
    int width = size.x;
    int height = size.y;
    
    int myGridColumns = width / myRequestedColumnWidth;
    
    if ( myGridColumns == 0 || myGridColumns == 1 )
        myGridColumns = 2;
    myColumnWidth = ( width / myGridColumns );
    
    myGridViewContentMain.setNumColumns(myGridColumns);
    

    following code was in layout xml for grid

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gridViewContentMain"
        android:layout_centerHorizontal="true"
        android:numColumns="2"
        android:columnWidth="200dp"
        android:verticalSpacing="0dp"
        android:layout_below="@+id/relativeLayoutMainHeader"
        android:layout_above="@+id/relativeLayoutProgressBar"
        />
    

    I was unable to solve issue after changing stretch mode values.

    But following value in layout xml for GridView did the trick.

        android:horizontalSpacing="0dp"
    

    I hope, someone like me may find this helpful.

    Regards.

    0 讨论(0)
  • 2021-02-04 05:36

    You can reduce the space b/w column by setting the android:horizontalSpacing="-10dp" ( some negative number )

    0 讨论(0)
  • 2021-02-04 05:37

    try to fix the number of columns needed in xml file for grid and set stretchMode as columnwidth

    android:numColumns="3"
    android:columnWidth="60dp"
    android:stretchMode="columnWidth"
    

    or set the horizontal and vertical spacing in xml for grid

    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    
    0 讨论(0)
提交回复
热议问题