protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); gv_home = (GridView) findViewById(R.id.gv_home); gv_home.setAdapter(new HomeAdapter()); //监听gridview点击事件 gv_home.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch(position){ case 8: //设置中心 startActivity(new Intent(HomeActivity.this, SettingActivity.class)); break; default: break; } } }); } GrideView的使用 首先创建布局文件 <?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 style=“@style/TitleStyle” android:text=“手机管家” /> <com.ldw.safe.view.FocusedTextView android:layout_width=“match_parent” android:layout_height=“wrap_content” android:textColor=“@color/black” android:text=“简书是一个优质的创作社区,在这里,你可以任性地创作,一篇短文、一张照片、一首诗、一幅画……我们相信,每个人都是生活中的艺术家,有着无穷的创造力” android:singleLine=“true” android:ellipsize=“marquee” android:layout_marginTop=“3dp” android:layout_marginBottom=“3dp” android:textSize=“18sp” /> <GridView android:id=“@+id/gv_home” android:layout_width=“match_parent” android:layout_height=“0dp” android:layout_weight=“1” android:verticalSpacing=“10dp” android:numColumns=“3” GrideView的单个条目 <?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/app” /> <TextView android:id=“@+id/tv_item” android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:text=“手机卫士” android:textColor=“@color/black” android:layout_marginTop=“5dp” android:textSize=“18sp” /> 逻辑代码,将单个条目逐一填充在GrideView中 package com.ldw.safe.Activity; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.ldw.safe.R; import com.ldw.safe.utils.MD5Utils; public class HomeActivity extends Activity{ private GridView gv_home; private String[] mItems = new String[] {“手机防盗”, “通讯卫士”, “软件管理”, “进程管理”, “流量统计”, “手机杀毒”, “缓存清理”, “高级工具”, “设置中心”}; private int[] mPics = new int[] {R.drawable.safe, R.drawable.callmsgsafe, R.drawable.app, R.drawable.taskmanager, R.drawable.netmanager, R.drawable.sysoptimize, R.drawable.trojan, R.drawable.atools, R.drawable.settings}; private SharedPreferences mPref; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); mPref = getSharedPreferences(“config”, MODE_PRIVATE); gv_home = (GridView) findViewById(R.id.gv_home); gv_home.setAdapter(new HomeAdapter()); //监听gridview点击事件 gv_home.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch(position){ case 0: //手机防盗 showPasswordDialog(); break; case 1: //通讯卫士 startActivity(new Intent(HomeActivity.this, CallSafeActivity.class)); break; case 2: //软件管理 startActivity(new Intent(HomeActivity.this, AppManagerActivity.class)); break; case 3: //进程管理 startActivity(new Intent(HomeActivity.this, TaskManagerActivity.class)); break; case 5: //手机杀毒 startActivity(new Intent(HomeActivity.this, AntivirusActivity.class)); break; case 7: //高级工具 startActivity(new Intent(HomeActivity.this, AdvancedToolsActivity.class)); break; case 8: //设置中心 startActivity(new Intent(HomeActivity.this, SettingActivity.class)); break; default: break; } } }); }