Android mobile guard-used by jiugongge, android jiugong
URL: http://www.cnblogs.com/wuyudong/p/5907736.html.
Use the GridView method. Similar to the ListView method, the number of columns (3 columns)
First add the GridView
<! -- Android: numColumns specifies the number of columns --> <! -- Android: verticalSpacing = "10dp" specifies the Vertical spacing of internal entries to 10dp --> <GridView android: id = "@ + id/gv_home" android: layout_width = "match_parent" android: layout_height = "match_parent" android: numColumns = "3" android: verticalSpacing = "10dp"> </GridView>
Data filling (Module name, module image)
Package com. wuyudong. mobilesafe. activity; import com. wuyudong. mobilesafe. r; import android. app. activity; import android. content. clipData. item; import android. OS. bundle; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. gridView; import android. widget. imageView; import android. widget. textView; public class HomeActivity extends Activity {private GridView gv_home; private String [] mTitleStrs; private int [] mDrawableIds; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_home); initUI (); // Method for initializing data initData ();} private void initData () {// prepare data (text (9 groups ), image (9 images) mTitleStrs = new String [] {"mobile anti-theft", "Communication guard", "software management", "Process Management", "Traffic Statistics ", "cell phone Antivirus", "cache cleanup", "advanced tools", "setting Center"}; mDrawableIds = 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}; // The data adapter (equivalent to the ListView data adapter) gv_home.setAdapter (new MyAdapter ();} private void initUI () {gv_home = (GridView) findViewById (R. id. gv_home);} class MyAdapter extends BaseAdapter {@ Override public int getCount () {// total number of text sets = number of image sheets return mTitleStrs. length ;}@ Override public Object getItem (int position) {return mTitleStrs [position] ;}@ Override public long getItemId (int position) {return position ;} @ Override public View getView (int position, View convertView, ViewGroup parent) {View view = View. inflate (getApplicationContext (), R. layout. gridview_item, null); TextView TV _title = (TextView) view. findViewById (R. id. TV _title); ImageView iv_icon = (ImageView) view. findViewById (R. id. iv_icon); TV _title.setText (mTitleStrs [position]); iv_icon.setBackgroundResource (mDrawableIds [position]); return view ;}}}
Create a layout file gridview_item.xml
<? 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_icon" android: background = "@ drawable/ic_launcher" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <TextView android: id = "@ + id/TV _title" android: text = "module title" android: textSize = "18sp" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> </LinearLayout>
The effect after running is as follows: