【问题标题】:Android: Reduce space between columns in GridViewAndroid:减少 GridView 中列之间的空间
【发布时间】:2011-11-17 12:59:09
【问题描述】:

请参阅我正在尝试减少 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>

我在我的代码中从位图创建 ImageView。但是,我没有指定任何填充或间距或其他内容。

如何减少列之间的空间?我在 GridViews 和 ImageViews 上尝试了各种设置,但都没有成功

图片是:

谢谢

【问题讨论】:

  • 嘿,我也遇到了同样的问题,不知道您是否找到了解决方案;你也是?
  • 您能接受指定列数而不是列宽的解决方案吗?

标签: android android-layout


【解决方案1】:

尝试固定xml文件中网格所需的列数并将stretchMode设置为columnwidth

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

或者在xml中为grid设置水平和垂直间距

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

【讨论】:

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

移除重力属性,使用以下模式

android:stretchMode="NONE"

【讨论】:

  • “*”是什么意思 - 这似乎不是允许的属性?无论哪种方式,当我尝试使用一些有效的属性值时它都不起作用
  • 代表* ( astrick ) 表示会自动调整空间
  • 这只是使所有列重叠。非常奇怪的行为。
【解决方案3】:

列之间的空间是因为你的gridview元素没有完全占据grid view提供的列空间。

去除列间距的解决方案是测量设备的屏幕宽度和高度,然后除以列数和行数。这将给出每个网格视图元素的宽度和高度。

您必须将此高度和宽度分配给 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

【讨论】:

    【解决方案4】:

    您好,我遇到了 mygridView.setStretchMode(GridView.NO_STRETCH); 解决的同样问题

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题。我通过这样做最小化了列空间

      对于网格视图

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

      对于图像视图

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

      【讨论】:

        【解决方案6】:

        我遇到了类似的问题,即在少数设备中的列之间存在轻微的水平间距。 (我不需要任何空间。)我通过将网格项宽度除以总宽度来正确计算网格项宽度。我只在联想设备上遇到了这个问题。

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

        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"
        

        我希望像我这样的人会觉得这很有帮助。

        问候。

        【讨论】:

          【解决方案7】:

          您可以通过设置 android:horizo​​ntalSpacing="-10dp" (一些负数)来减少空间 b/w 列

          【讨论】:

            猜你喜欢
            • 2014-03-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-08-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-10-22
            相关资源
            最近更新 更多