在android中的gridview中重叠的图像

时间:2014-03-15 09:41:35

标签: android android-gridview

我正在创建gridview来显示图像。即使我设置垂直和水平间距,静止图像也会相互重叠。为什么会这样?以及如何解决这个问题?你可以在图像中看到这一点。

我的代码:

public class Images extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.images);

        GridView gridView = (GridView) findViewById(R.id.grid_view);
        gridView.setAdapter(new ImageAdapter(this));
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                i.putExtra("id", position);
                startActivity(i);
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {

        private Context mContext; 
        int width, height;

        public Integer[] mThumbIds = {
                R.drawable.radhe, R.drawable.radhe,
                R.drawable.radhe, R.drawable.radhe,
                R.drawable.radhe, R.drawable.radhe,
                R.drawable.radhe, R.drawable.radhe,
                R.drawable.radhe
        };

        // Constructor
        public ImageAdapter(Context c){
            mContext = c;
        }

        @Override
        public int getCount() {
            return mThumbIds.length;
        }

        @Override
        public Object getItem(int position) {
            return mThumbIds[position];
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);
            DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            width = displaymetrics.widthPixels;
            height = displaymetrics.heightPixels;
            imageView.setLayoutParams(new GridView.LayoutParams(width/3, height/3));
            imageView.setScaleType(ScaleType.FIT_XY);
            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="3"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="5dp"
    android:gravity="center"
    android:stretchMode="columnWidth" >  

</GridView>

这是我得到的输出。

enter image description here

2 个答案:

答案 0 :(得分:0)

使用下面的代码片段代替您使用的代码:

<GridView 
    android:id="@+id/gv_Grid" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:columnWidth="90dp"
    android:numColumns="3" 
    android:verticalSpacing="60dip"
    android:horizontalSpacing="10dip"
    android:stretchMode="columnWidth"
    android:gravity="center" 
    android:layout_below="@+id/rr_grid_titleBar" /> 

由于

答案 1 :(得分:0)

请在getView方法(网格适配器)中使用imageView.setPadding(20, 20, 20, 20);。填充为网格项(图像视图)提供一些空间,它们看起来是分开的。