以编程方式删除GridView周围的多余空间



我正试图在我的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)
    );
  • 确保填充在不同密度的屏幕上显示相同的一种方法是将其转换为DIP单元。

    int padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -5, 
                                        getResources().getDisplayMetrics());
    
  • 您还可以尝试在xml中定义一个null drawable。。

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

    然后呼叫setSelector(R.drawable.null_drawable);

更新:

  • 在自己的xml中定义GridView并对其进行扩展。

    layout/mygrid.xml

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

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

通过以下方式解决

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

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

要获得与使用android:listSelector="@null"相同的效果,但在代码中需要使用setSelector(),如marvinXXII所述。但是您需要传入一个有效的ResourceId或使用setSelector(Drawable sel)变体。不幸的是,不可能将null传递到该方法中,因为这将导致NullPointerException。这里描述的解决方法是使用以下内容:

gridView.setSelector(android.R.color.transparent);

我认为您可以在创建动态网格视图时使用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);

setSelector(int)(来自AbsListView)不是您想要的吗?

类似setSelector(0)

你看到这个问题了吗?为什么我的Android GridView周围有额外的像素??在这种情况下,问题是用作网格视图选择器的图像有填充。

通过编程应用该问题的建议解决方案,您可以设置选择器

gridView.setSelector(android.R.color.transparent)

使用以下设置对我有效:

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和重力设置为CENTER_HORIZONTAL的组合。

最新更新