我想删除出现在 GridView 中的额外填充。我有大小为 128*128 的图像,它们将占据网格中的单元格。但不知何故,有一个额外的空间被添加到网格的内容中。
经过一番研究,我能够确定我必须覆盖 GridView 的 listSelector 属性。现在这是我的问题 - 我知道我必须在这里指定类似 xml drawable 的东西,但是在其中指定什么?我尝试使用可绘制的形状,并将填充和笔画设置为 0dp,但无济于事。
问题被问及回答 here ,但他们没有给出可绘制对象必须包含的内容。
有人可以帮我解决这个问题吗?谢谢!
编辑:好的 - here是我拥有的 UI 的副本。同样的XML布局如下:
<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。这是它的代码:
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;
}
}
你能找出错误吗?
注意:最后我在表格布局中实现了一个类似的屏幕,它呈现了更好的网格。
最佳答案
是的,我遇到了同样的问题。您想将 listSelector 设置为 @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" />
另外,请尝试以下操作:
myGridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
我知道你可以在 XML 中做到这一点,但当我遇到同样的问题时我没有这样做;不知道为什么。
我还硬编码了键高:
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);
}
}
很抱歉没有给出准确的答案,如果您在那之后仍然需要帮助,请告诉我。
关于android - 在 android 的 GridView 中删除额外的填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6876861/