因为突发奇想,发现网上的例子的网格布局,只有图片的实例,没有加文字说明! 后来找到个是来自《Android核心技术与实例详解》一书的实例 ,使用的适配是SimpleAdapter,而我想用继承BaseAdapter类,重写getView方法.经过自己的资料的查阅与调试 ,完成了个小例子;
布局页面代码 有个layout
gridView的子视图,也就是每一个网格的内容页面布局
然后在res/drawable 放入9张图片资源 Activity页面代码
public class WidgetActivity extends Activity { OnItemClickListener ocl_gridview = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setTitle("九宫格布局"); GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this)); gridview.setOnItemClickListener(ocl_gridview); } public void bindListener() { ocl_gridview = new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { } }; } }继承BaseAdapter的ImageAdapter类代码
public class ImageAdapter extends BaseAdapter{ private Context mContext; public ImageAdapter(Context c) { mContext = c; } @Override public int getCount() { // TODO Auto-generated method stub return mThumbIds.length; } @Override public View getView(int position, View convertView, ViewGroup arg2) { View view = View.inflate(mContext, R.layout.relagrid, null); RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.relaGrid); ImageView image = (ImageView) rl.findViewById(R.id.chooseImage); TextView text = (TextView) rl.findViewById(R.id.chooseText); image.setImageResource(mThumbIds[position]); text.setText(mTextValues[position]); return rl; } // references to our images private Integer[] mThumbIds = { R.drawable.ph1,R.drawable.ph2,R.drawable.ph3, R.drawable.ph4,R.drawable.ph5,R.drawable.ph6, R.drawable.ph7,R.drawable.ph8,R.drawable.ph9 }; private String[] mTextValues = { "Button","TextView","EditText", "CheckBox","Radio","Spinner", "","","" }; }
第一次发帖,可能有很多不足,希望大家见谅!
放张效果图给大家look
转载:http://www.adobex.com/android/source/details/00000444.htm