MainActivity.java
package cn.edu.bzu.grid.activity; import com.example.grid.R; import cn.edu.bzu.grid.model.ImageAdapter; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.ImageView; public class MainActivity extends Activity { GridView gridView; ImageView imageView; private Integer[] integer={ R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6, }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView)findViewById(R.id.GV01); imageView = (ImageView)findViewById(R.id.IV01); gridView.setAdapter(new ImageAdapter(this)); //gridView.setBackgroundResource(R.drawable.ic_launcher); gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { imageView.setImageResource(integer[arg2]); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }ImageAdapter.java
package cn.edu.bzu.grid.model; import com.example.grid.R; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.GridView; import android.widget.ImageView; @SuppressWarnings("deprecation") public class ImageAdapter extends BaseAdapter{ private Context context; private Integer[] integer={ R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6 }; public ImageAdapter(Context context){ this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return integer.length; } @Override public Object getItem(int positoin) { // TODO Auto-generated method stub return positoin; } @Override public long getItemId(int positoin) { // TODO Auto-generated method stub return positoin; } @Override public View getView(int positoin, View arg1, ViewGroup parent) { // TODO Auto-generated method stub ImageView imageView = new ImageView(context); imageView.setImageResource(integer[positoin]); imageView.setLayoutParams(new GridView.LayoutParams(90, 70)); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); return imageView; } }
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent"> <GridView android:id="@+id/GV01" android:layout_width="fill_parent" android:layout_height="0dp" android:columnWidth="80dp" android:gravity="center" android:horizontalSpacing="10dp" android:numColumns="2" android:stretchMode="spacingWidthUniform" android:verticalSpacing="10dp" android:layout_weight="2"> </GridView> <ImageView android:id="@+id/IV01" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>