安卓GridView总结

1、http://blog.csdn.net/hellogv/article/details/4567095  基础篇,GridView最基本的用法

2、http://my.eoe.cn/cainiao1/archive/4038.html 基础篇,继承BaseAdapter

3、http://blog.chengyunfeng.com/?p=465 自适应屏幕的GridView

4、http://www.eoeandroid.com/thread-172021-1-1.html eoe关于GridView用法的集合


手机卫士gridView代码实例:

    主布局文件:

        

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#8866ff00"
        android:gravity="center"
        android:text="功能列表"
        android:textColor="@color/black"
        android:textSize="22sp" />

    <GridView
        android:id="@+id/gv_home"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:numColumns="3"
        android:verticalSpacing="20dp" >
    </GridView>

</LinearLayout>

Item布局文件:

<?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_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/home_apps" />

    <TextView
        android:id="@+id/tv_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textColor="@color/black"
        android:textSize="18sp"
        android:text="手机防盗" />

</LinearLayout>

java代码:      

public class HomeActivity extends Activity {

	private GridView gvHome;

	private String[] mItems = new String[] { "手机防盗", "通讯卫士", "软件管理", "进程管理",
			"流量统计", "手机杀毒", "缓存清理", "高级工具", "设置中心" };

	private int[] mPics = new int[] { R.drawable.home_safe,
			R.drawable.home_callmsgsafe, R.drawable.home_apps,
			R.drawable.home_taskmanager, R.drawable.home_netmanager,
			R.drawable.home_trojan, R.drawable.home_sysoptimize,
			R.drawable.home_tools, R.drawable.home_settings };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);

		gvHome = (GridView) findViewById(R.id.gv_home);
		gvHome.setAdapter(new HomeAdapter());
	}

	class HomeAdapter extends BaseAdapter {

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

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

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = View.inflate(HomeActivity.this,
					R.layout.home_list_item, null);
			ImageView ivItem = (ImageView) view.findViewById(R.id.iv_item);
			TextView tvItem = (TextView) view.findViewById(R.id.tv_item);

			tvItem.setText(mItems[position]);
			ivItem.setImageResource(mPics[position]);
			return view;
		}

	}
}

 备注:去掉GridView难看的点击背景色,accountView.setSelector(new ColorDrawable(Color.TRANSPARENT));

猜你喜欢

转载自blog.csdn.net/kai_zone/article/details/80204214
今日推荐