Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中如何消除网格视图行间距_Android_Android Gridview - Fatal编程技术网

android中如何消除网格视图行间距

android中如何消除网格视图行间距,android,android-gridview,Android,Android Gridview,在android网格视图中,网格视图的行之间有额外的空间。我没有向gridview添加任何空间 这是我的xml文件 <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/grid" android:layout_width="fill_parent"

在android网格视图中,网格视图的行之间有额外的空间。我没有向gridview添加任何空间

这是我的xml文件

        <GridView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/grid"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:horizontalSpacing="5px"
                android:numColumns="3"
                android:columnWidth="50dip"
                 android:verticalSpacing="0dip"
                android:gravity="center"
                />
空间位于gridview的行之间。我没有在代码中的任何地方添加任何空格,仍然有垂直间距。我不理解为什么gridview行之间有空格。此空间未出现在hdpi设备/仿真器中。mdpi和ldpi设备/仿真器中存在此问题


在您的getView()方法中放置以下行

mImage.setPadding(0, 0, 0, 0);

我希望这将对您有所帮助。

看起来您的图片正在缩放,以适应50dp的列宽,同时保持纵横比。这将在垂直布局中引入一组未填充的空间。如果您希望列的宽度为50dp,解决这些间距问题的最简单方法是将imageview的宽度调整为50dp,并将imageview的高度与此成比例。

只需检查“newsgridthumbpic”布局不包含额外的填充或网格视图元素高度较大的任何背景图像。

请再次检查是否提供列宽为50度,但“imageview”的宽度为145度。

我找到了此问题的解决方案

为了解决这个问题,我需要在xml文件中添加一行

android:adjustViewBounds="true"

加上这个。空间现在不显示

将gridview中的android:verticalSpacing设置为负值。 将删除垂直空间

(例如:android:verticalSpacing=“-10dp”)

使用一些数字进行测试以获得正确的值


<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:horizontalSpacing="5px"
    android:numColumns="3"
    android:columnWidth="50dip"
     android:verticalSpacing="0dip"
    android:gravity="center"/>

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/singlePhoto"
    android:layout_width="145dip"
    android:layout_height="145dip"
    android:scaleType="fitXY">
</ImageView>

只需将最后两行放入gridView即可

<GridView
        android:numColumns="2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/gridView"
        android:horizontalSpacing="0dip"
        android:verticalSpacing="0dip"/>

我有另一种方法来修复相同的问题,而另一种方法是使用RecyclerView

只需像我在GridLayoutManager中给出的6一样传递SpanCount的数量(这6个),然后更改SpanCount以减少或增加RecyclerView中项目之间的差距

检查此示例代码-

GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 6);
  rvSizes.setLayoutManager(gridLayoutManager);
  SizeSelectorAdaptersizeChooserAdapter = new SizeSelectorAdapter(this, sizes);
  rvSizes.setAdapter(sizeChooserAdapter);
希望这将是有用的。
谢谢

即使我将其更改为dip,它也会显示行与行之间的空间,问题是关于垂直间距放置在网格中的视图的高度定义网格高度。你的重力被设置为“顶部”,这是导致你的网格看起来像这样的原因。是的,我尝试了所有改变图像视图高度和宽度的方法,图像来自服务器,这就是为什么没有背景或src图像。你是否将此添加到grivview或网格项?这似乎对我不起作用:(将此添加到imageview,对我有效…Thanx Dipali重要提示:如Anchit所述,这应该放在imageview中。您好,Dipali它对我无效…您能告诉我如何解决此问题吗?在我的情况下,我有两列,我不希望图像之间有任何空格…但在xml文件中的何处?在GridView中或任何其他地方?请告诉我。)提供一个简短的解释,而不仅仅是代码。android:scaleType=“fitXY”是帮助解决问题的属性
<GridView
        android:numColumns="2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/gridView"
        android:horizontalSpacing="0dip"
        android:verticalSpacing="0dip"/>
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 6);
  rvSizes.setLayoutManager(gridLayoutManager);
  SizeSelectorAdaptersizeChooserAdapter = new SizeSelectorAdapter(this, sizes);
  rvSizes.setAdapter(sizeChooserAdapter);