以编程方式删除GridView周围的额外空间

8

我正试图在我的Java类中以编程方式创建一个GridView,一切都很顺利。问题是GridView周围自动生成的5像素填充。在xml中,我使用以下方法将其移除:

android:listSelector="@null"

但是我无法在Java中做到类似的事情。我尝试过一些解决方法,比如将 GridView 的大小增加10像素,但没有成功。

有人有这方面的代码吗?

编辑:

我提供的答案并没有解决问题。仍然有赏金可获得。下面是我的GridView 代码:

    GridView gridView = new GridView(this);
    gridView.setNumColumns(someInt);
    gridView.setAdapter (new MyCustomAdapter(this));
    gridView.setLayoutParams(new GridView.LayoutParams(
            customValue,
            LayoutParams.FILL_PARENT,
            Gravity.CENTER_HORIZONTAL)
    );
7个回答

6
  • One way to ensure the padding appears same on screens with different density is by converting it to DIP units.

    int padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -5, 
                                        getResources().getDisplayMetrics());
    
  • Other thing you can try is to define a null drawable in xml..

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <drawable name="null_drawable">@null</drawable>
        ...
    </resources>
    

    Then call setSelector(R.drawable.null_drawable);

更新:

  • Define your GridView in its own xml and inflate it.

    layout/mygrid.xml

    <?xml version="1.0" encoding="utf-8"?>
    <GridView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:listSelector="@null"/>
    

    In java,

    GridView gridView = (GridView)inflater.inflate(R.layout.mygrid, null);
    gridView.setLayoutParams(new GridView.LayoutParams(customValue, 
                            LayoutParams.FILL_PARENT));
    gridView.setNumColumns(someInt);
    gridView.setAdapter (new MyCustomAdapter(this));
    

它崩溃了?还是语法错误?错误是什么? - Ron
我曾考虑过那个解决方案,但我想检查是否有任何100%以编程方式完成而不使用xml的方法,因为将多个xml导入类通常是不稳定的(这是我的经验)。 - Magakahn
你在解压缩时遇到了什么问题?“将多个XML导入类”是什么意思? - Ron
但我认为在Java中现在充气和创建之间没有任何区别,因为您将充气的视图是一个单一的视图(而不是深层次的视图层次结构,这使得充气成本高昂)... 因此,您可以放心使用此选项而没有任何缺点。 - Ron
我会给这个问题点赞,但是我不接受它,因为它没有完全回答我的问题。换句话说,回答很好,但不能完全以编程方式解决。由于你的点赞,你也会得到大约50的悬赏。看起来我必须无论如何使用这个解决方案。 - Magakahn
显示剩余2条评论

2

我通过以下方式解决了问题:

    gridView.setPadding(-5, 0, -5, 0);

但是不同的屏幕需要不同的填充。但这并不是一个完整的解决方案。此代码的功能修改将被接受。


2
与使用android:listSelector="@null"相同的效果,但是在代码中需要使用setSelector(),正如marvinXXII所提到的那样。 但是您需要传递一个有效的ResourceID或使用setSelector(Drawable sel)变体。 不幸的是,不可能将null传递给该方法,否则会导致NullPointerException。 此处描述的解决方法在这里
gridView.setSelector(android.R.color.transparent);

虽然有所改善,但在 GridView 的侧面仍然是像素。 - Magakahn

1

我认为在创建动态网格视图时,您可以使用setPadding来解决这个问题。

int grids_height = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, (int) (Row_num * 80),
            getResources().getDisplayMetrics());
    Log.v("", "" + grids_height);
    gridview = new GridView(this);
    gridview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            grids_height));
    gridview.setPadding(0, 3, 0, 0);
    gridview.setBackgroundColor(Color.TRANSPARENT);
    gridview.setNumColumns(7);
    gridview.setVerticalSpacing(1);
    gridview.setHorizontalSpacing(1);
    gridview.setGravity(Gravity.CENTER_HORIZONTAL);
    gridview.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);

不好。我已经尝试过很多次使用setPadding,而且我必须为不同的屏幕设置不同的padding以适应。而且安卓设备有很多不同的屏幕尺寸。 - Magakahn

0

0

我使用以下设置可以正常工作:

public ContactContainer(Context context, AttributeSet attrs) {
    super(context, attrs);
    gallery = new GridView(context, attrs);
    gallery.setNumColumns(numColumns);
    gallery.setStretchMode(stretchMode);
    gallery.setSmoothScrollbarEnabled(smoothScrollbar);
    gallery.setSelector(listSelector);
    gallery.setGravity(Gravity.CENTER_HORIZONTAL);
    this.addView(gallery);
}

我将它与这段 XML 代码结合使用:

<de.ramicro.SClip.components.ContactContainer
    android:id="@+id/gridview_gallery_contacts"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/contact_gallery_bottom_bar"
    android:layout_below="@id/contact_gallery_top_bar"
    android:padding="0dip"/>

我认为你需要的可能是将Selector设置为透明并将gravity设置为CENTER_HORIZONTAL的组合。


请上传您的stretchMode、smoothScrollbar和listSelector。 - Magakahn
私有整型变量listSelector = android.R.color.transparent; 私有整型变量numColumns = 2; 私有布尔变量smoothScrollbar = true; 私有整型变量stretchMode = GridView.STRETCH_COLUMN_WIDTH; - Eduardo

0

setSelector(int)(来自AbsListView)难道不是你要找的吗?

就像setSelector(0)一样


不好。导致崩溃或文件错误。要求我更正为setSelected。 - Magakahn
不应该这样,你能展示一下GridView初始化的代码吗? - marwinXXII
如果程序真的崩溃了,那么需要堆栈跟踪来理解为什么会崩溃。 - marwinXXII
它崩溃了,因为0不是有效的资源ID。 - Renard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接