<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/iv_image" android:layout_width="wrap_content" android:layout_height="60dp" android:src="@drawable/app" /> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置中心" /> </LinearLayout>
<GridView android:id="@+id/grid_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numColumns="3" > </GridView>
protected class MyGridAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return titles.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view = View.inflate(MainActivity.this, R.layout.list_item_main, null); TextView title = (TextView) view.findViewById(R.id.tv_title); title.setText(titles[position]); return view; }
private GridView grid_main; private MyGridAdapter adapter; private static String []titles = {"设置中心1","设置中心2","设置中心3","设置中心4","设置中心5","设置中心6","设置中心7","设置中心8","设置中心9"}; .... grid_main = (GridView) findViewById(R.id.grid_main); adapter = new MyGridAdapter(); grid_main.setAdapter(adapter);
grid_main.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub ConsoleItemClick(position); } });
原文:http://www.cnblogs.com/xuhongfei/p/4009879.html