手机安全卫士开发系列(6)——程序主界面
主界面的布局文件
<?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:background="@color/backgroundcolor" android:orientation="vertical" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="40dip" android:background="@drawable/title_background" android:gravity="center_horizontal|center_vertical" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF8F8F8" android:textSize="22sp" android:text="山寨手机卫士" /> </LinearLayout> <GridView android:id="@+id/gv_main" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dip" android:horizontalSpacing="10dip" android:numColumns="3" android:verticalSpacing="10dip" > </GridView> </LinearLayout>
适配器
package cn.itcast.mobilesafe.adapter; import cn.itcast.mobilesafe.R; import android.content.Context; import android.content.SharedPreferences; import android.util.Log; 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 MainUIAdapter extends BaseAdapter { private static final String TAG = "MainUIAdapter"; private Context context; private LayoutInflater inflater; private static ImageView iv_icon; private static TextView tv_name; private SharedPreferences sp; public MainUIAdapter(Context context) { this.context = context; inflater = LayoutInflater.from(context); sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); } private static String[] names = { "手机防盗", "通讯卫士", "软件管理", "任务管理", "流量管理", "手机杀毒", "系统优化", "高级工具", "设置中心" }; private static int[] icons = { R.drawable.widget05, R.drawable.widget02, R.drawable.widget01, R.drawable.widget07, R.drawable.widget05, R.drawable.widget04, R.drawable.widget06, R.drawable.widget03, R.drawable.widget08 }; public int getCount() { return names.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { // getview的方法被调用了多少次? // 9 // gridview 控件bug // won't fix // 使用静态的变量引用 减少内存中申请的引用的个数 Log.i(TAG,"getview "+ position); View view = inflater.inflate(R.layout.mainscreen_item, null); iv_icon = (ImageView) view.findViewById(R.id.iv_main_icon); tv_name = (TextView) view.findViewById(R.id.tv_main_name); iv_icon.setImageResource(icons[position]); tv_name.setText(names[position]); if(position==0){ String name = sp.getString("lost_name", null); if(name!=null){ tv_name.setText(name); } } return view; } }
mainscreen_item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dip" android:layout_height="100dip" android:gravity="center_horizontal" android:background="@drawable/item_background" android:orientation="vertical" > <ImageView android:id="@+id/iv_main_icon" android:layout_width="60dip" android:layout_height="60dip" android:scaleType="fitXY" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv_main_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="功能名字" android:textColor="@color/textcolor" android:textSize="18sp" /> </LinearLayout>
样式文件
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <stroke android:width="0.5dip" android:color="#ff505050" /> <corners android:radius="2dip" > </corners> <gradient android:startColor="#ff404040" android:centerColor="#ff383838" android:endColor="#ff404040" /> </shape>
效果