2011-09-15 207 views
13

请参阅附件截图,我试图减少GridView中列之间的空间。Android:减少GridView中列间的空间

我的main.xml如下:

<?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="fill_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> 

我从位图建立在我的代码ImageViews。但是,我没有指定任何填充或间距或其他。

如何减少列之间的空间?我已经尝试了多种设置在两个GridView和ImageViews,但没有运气

图片here

感谢

+0

过去的imageadapter代码: –

+4

嘿那里,我有同样的问题,我想知道如果你找到任何解决方案;你呢? – Ciprian

+0

您能否接受指定列数而不是列宽的解决方案? – Hong

回答

19

尝试修复在XML文件中需要的网格列数,并设置stretchMode作为columnWidth时

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

或设置水平和垂直间距以xml电网

android:verticalSpacing="10dp" 
android:horizontalSpacing="10dp" 
+3

我实际上已经在我的代码中设置了numColumns。这些选项都没有减少列之间的空间。将水平间距修改为负值只是将整个网格向左推进。我可以让空间消失的唯一方法是如下宽度硬编码:android:layout_width =“350px”。但是,这不是我的首选解决方案 – DJ180

+0

不起作用.. – Ixx

11

删除重力属性并使用下面的模式列之间

android:stretchMode="NONE" 
+0

你是什么意思的“*” - 这似乎不是一个允许的属性?无论哪种方式,当我尝试这与一些有效的属性值它不起作用 – DJ180

+0

它代表*(astrick)意味着自动调整空间 – sravan

+0

不起作用。 – Ixx

4

空间,因为你的GridView元件没有被完全占据由网格视图中提供的列空间。

删除列之间空间的解决方案是测量设备的屏幕宽度和高度,并将其除以列数和行数。这将给每个网格视图元素的宽度和高度。

您必须将此高度和宽度分配给imageview,并缩放您的位图以匹配imageview。

代码:

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 
3

嗨,我有我的mygridView.setStretchMode(GridView.NO_STRETCH);

1

解决了我在具有几台设备中的列之间的轻微水平间距的类似的问题,同样的问题。 (我没有要求任何空间。)我正在计算网格项目的宽度除以总宽度。我只在Lenovo设备上收到此问题。

以下代码与网格相关的活动。

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

下面的代码是在布局XML电网

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

我无法改变拉伸模式值后要解决的问题。

但是,下面的GridView布局xml中的值做了诀窍。

android:horizontalSpacing="0dp" 

我希望,像我这样的人可能会觉得这有帮助。

问候。

1

我有同样的问题。我做这个

为GridView的

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

为ImageView的

android:layout_width="115dp" 
android:layout_height="150dp" 
android:padding="1dp" 
android:layout_marginLeft="30dp" 
android:layout_marginRight="-15dp" 
0

最小柱空间,可以通过设置在Android减少空间的B/W列:horizo​​ntalSpacing = “ - 10dp”(一些负数)

相关问题