我的手机管家
主界面布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:text="安全应用中心" android:textColor="#ffffff" android:background="#32921C" android:textSize="24sp" android:gravity="center_horizontal" android:textAppearance="?android:attr/textAppearanceLarge" /> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="300dp" android:layout_below="@id/textView1" android:src="@drawable/d" /> <GridView android:gravity="center" android:id="@+id/gridView1" android:background="#FFFFFF" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/imageView1" android:layout_alignParentBottom="true" android:numColumns="3" > </GridView> </RelativeLayout>
效果图:底部是一个GridView 显示管理应用的功能项,
共有9个功能:
自定义适配器:
package com.chb.myphonesave.adapter; import java.util.List; import java.util.Map; import com.chb.myphonesave.R; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class MyGrdViewAdapter extends BaseAdapter { private List<Map<String, Object>> list; private LayoutInflater lInflater; public MyGrdViewAdapter(Context context, List<Map<String, Object>> list){ lInflater = LayoutInflater.from(context); this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHandler vHandler = null; if(convertView == null){ vHandler = new ViewHandler(); convertView = lInflater.inflate(R.layout.gv_item, null); vHandler.iv = (ImageView)convertView.findViewById(R.id.gv_item_iv); vHandler.tv = (TextView)convertView.findViewById(R.id.gv_item_tv); convertView.setTag(vHandler); }else { vHandler = (ViewHandler) convertView.getTag(); } int gv_item_iv = (Integer) list.get(position).get("gv_item_iv");//图片资源id String gv_item_tv = (String) list.get(position).get("gv_item_tv");//文字 vHandler.iv.setBackgroundResource(gv_item_iv); vHandler.tv.setText(gv_item_tv); return convertView; } //优化缓存 private class ViewHandler{ ImageView iv; TextView tv; } }
主界面逻辑代码:
package com.chb.myphonesave.activity; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import com.chb.myphonesave.R; import com.chb.myphonesave.adapter.MyGrdViewAdapter; public class MainActivity extends Activity { private GridView gridView; private MyGrdViewAdapter adapter; private String[] items; //item字符串 private int[] gridview_imgs; //item图片 private List<Map<String, Object>> item_list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);//无标题 setContentView(R.layout.activity_main); initView(); } /** * 初始化组件 */ private void initView() { String[] items = getResources().getStringArray(R.array.gridview_item);//获取名称数组 gridview_imgs = new int[] {//gridView item 的图片 R.drawable.process_manager,R.drawable.cache_clean,R.drawable.app, R.drawable.blacknumber,R.drawable.antivirus,R.drawable.traffic, R.drawable.safe,R.drawable.settings,R.drawable.exit_icon }; item_list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < items.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("gv_item_tv", items[i]); map.put("gv_item_iv", gridview_imgs[i]); item_list.add(map); } gridView = (GridView) findViewById(R.id.gridView1); adapter = new MyGrdViewAdapter(this, item_list); gridView.setAdapter(adapter); gridView.setOnItemClickListener(listener); } /** * gridView item事件的监听 */ private OnItemClickListener listener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //根据点击事件调到相应的功能界面 Log.i("tag", "item"+position+"被点击了"+item_list.get(position).get("gv_item_tv")); } }; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
在res/strings中定义一个<string-array>用来存储9个功能名称
<string-array name="gridview_item" > <item > <string name="progress_control">进程管理</string> </item> <item> <string name="clean_rabish">垃圾清理</string> </item> <item> <string name="app_control">应用管理</string> </item> <item> <string name="saoraolanjie">骚扰拦截</string> </item> <item> <string name="bingdusaomiao">病毒扫描</string> </item> <item> <string name="wangluozhushou">网络助手</string> </item> <item> <string name="shoujifangdao">手机防盗</string> </item> <item> <string name="shezhizhongxin">设置中心</string> </item> <item> <string name="exit">退出应用</string> </item> </string-array>
在逻辑代码中通过
String[] items = getResources() .getStringArray(R.array.gridview_item);//获取名称数组
时间: 2024-10-08 19:05:25