开发者

Android: Reduce space between columns in GridView

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

My main.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView 
android:id="@+id/gridview"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:stretchMode="columnWidth"
android:gravity="center"
/>
<LinearLayout 
android:orientation="horizontal"
android:layout_width="fi开发者_如何学Cll_parent"
android:layout_height="fill_parent">
    <ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/logo"/>
    <TextView
    android:text="@string/game_score"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>

I am creating the ImageViews in my code from a Bitmap. However, I am not specifying any padding or spacing or otherwise.

How can I reduce this space between columns? I've tried a variety of settings on both GridViews and ImageViews but with no luck

Image is

Android: Reduce space between columns in GridView

:

Thanks


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"


Remove Gravity attribute and use the following mode

android:stretchMode="NONE"


Space between columns is because your gridview element is not completely occupying the column space provided by grid view.

The solution to remove space between columns is to measure the device's screen width and height and divide it by the number of columns and rows. This will give the width and height for each grid view element.

you have to assign this height and width to imageview and also scale your bitmap to match the imageview.

Code:

  DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
  screen_width=displayMetrics.widthPixels;    //width of the device screen
  screen_height=displayMetrics.heightPixels;   //height of device screen

     int view_width=screen_width/number of columns;   //width for imageview
     int view_height=screen_height/number of rows;   //height for imageview


   Imageview myImageView=(ImageView)findViewById(R.id.my_id);
   myImageView.getLayoutParams().width=view_width;
   myImageView.getLayoutParams().height=view_height;

   Bitmap myImage=Bitmap.createScaledBitmap(src, view_width, view_height, true);//scaling the image to match the size of image view


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


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"


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜