在 android 中删除 GridView 中的额外填充 [英] Removing the extra padding in a GridView in android

查看:26
本文介绍了在 android 中删除 GridView 中的额外填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除出现在网格视图中的额外填充.我有大小为 128*128 的图像,它们将占据网格中的单元格.但不知何故,有一个额外的空间被添加到网格的内容中.

I want to remove the extra padding that appears in a grid view. I have images of the size 128*128 which will be occupying cells in the grid. But somehow there is an extra space that gets added to the contents of the grid.

经过一些研究,我确定我必须覆盖网格视图的 listSelector 属性.现在这是我的问题 - 我知道我必须在这里指定一个 xml drawable 之类的东西,但是要指定什么?我尝试使用填充和笔触设置为 0dp 的可绘制形状,但无济于事.

After some research, I was able to determine that I have to override the listSelector property of the grid view. Now here's my question - I know I have to specify something like an xml drawable here, but what to specify in that?? I tried using a shape drawable with padding and stroke set to 0dp to no avail.

此处提出并回答问题,但他们没有给出 drawable 必须包含的内容.

The question is asked and answered here, but they haven't given what the drawable must contain.

有人可以帮我解决这个问题吗?谢谢!

Can some one help me with this. Thanks!

好的 - 这里是我拥有的 UI 副本.并且相同的 XML 布局如下:

Ok - here's a copy of the UI that I have. And the XML layout for the same is as follows:

<GridView android:id="@+id/GV_A2ZMenu" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:numColumns="4"
            android:layout_gravity="top" android:stretchMode="columnWidth"
    android:gravity="center" android:listSelector="@null" />

我正在使用 BaseAdapter 类来填充 gridView.这是它的代码:

And I am using a BaseAdapter class to populate the gridView. Here's its code:

public class AtoZMenu extends BaseAdapter {

private static Context AppC;

private Integer[] MenuImg = { R.drawable.alphabet_a, R.drawable.alphabet_b,
        R.drawable.alphabet_c, R.drawable.alphabet_d,
        R.drawable.alphabet_e, R.drawable.alphabet_f,
        R.drawable.alphabet_g, R.drawable.alphabet_h,
        R.drawable.alphabet_i, R.drawable.alphabet_j,
        R.drawable.alphabet_k, R.drawable.alphabet_l,
        R.drawable.alphabet_m, R.drawable.alphabet_n,
        R.drawable.alphabet_o, R.drawable.alphabet_p,
        R.drawable.alphabet_q, R.drawable.alphabet_r,
        R.drawable.alphabet_s, R.drawable.alphabet_t,
        R.drawable.alphabet_u, R.drawable.alphabet_v,
        R.drawable.alphabet_w, R.drawable.alphabet_x,
        R.drawable.alphabet_y, R.drawable.alphabet_z };

public AtoZMenu(Context C) {
    AppC = C;
}

public int getCount() {
    return MenuImg.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView IV;
    float density = AppC.getResources().getDisplayMetrics().density;

    if (convertView == null) {
        IV = new ImageView(AppC);
        IV.setMaxHeight((int) (1));
    } else {
        IV = (ImageView) convertView;
    }
    IV.setImageResource(MenuImg[position]);
    return IV;
}
 }

你能发现错误吗?

注意:最后,我在表格布局中实现了一个类似的屏幕,从而呈现出更好的网格.

Note: In the end I ended up implementing a similar screen in a table layout which renders much better grids.

推荐答案

是的,我遇到了同样的问题.您想将 listSelector 设置为 @null:

Yep, I've had the same problem. You want to set the listSelector to @null:

<!-- Setting the listSelector to null removes the 5px border -->
<GridView
    android:id="@+id/view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:listSelector="@null" />

另外,请尝试以下操作:

Also, try the following:

myGridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);

我知道您可以在 XML 中执行此操作,但是当我遇到同样的问题时我没有;不知道为什么.

I see you can do this in the XML, but I didn't when I had this same problem; not sure why.

我还对关键高度进行了硬编码:

I also hard-coded the key height:

float density = getContext().getResources().getDisplayMetrics().density;
mKeyHeight = (int) (56 * density);
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageButton b = (ImageButton) convertView;
    if (b == null) {
        b = new ImageButton(getContext());
        b.setMinimumHeight(mKeyHeight);
        b.setBackgroundResource(R.drawable.btn_keyboard_key);
        b.setOnClickListener(this);
    }
}

很抱歉没有给出准确的答案,如果您在那之后还需要帮助,请告诉我.

Sorry about not giving a precise answer, so let me know if you still need help after that.

这篇关于在 android 中删除 GridView 中的额外填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆