手机卫士知识点二----主界面 gridview.

1.主布局.item布局   适配器  .2主界面3.engine.

<?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      
       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:listSelector="@drawable/item_background_selector"
        android:id="@+id/gv_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dip"
        android:numColumns="3">
    </GridView>
</LinearLayout>
//在主布局的在gridview可以加参数horizontalSpacing和vertivalSpacing表示gridview控件之间的的间隔.
<?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:paddingTop="10dip"
    android:paddingBottom="10dip"
    android:background="@drawable/item_background_selector"
    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文件来写一个

<?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>

stroke里面指定边框的颜色  边框的宽度,也就是线条的宽度

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.
		sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
	}
//得到字符串数组
	private static String[] names = { "手机防盗", "通讯卫士", "软件管理", "任务管理", "流量管理",
			"手机杀毒", "系统优化", "高级工具", "设置中心" };
	//得到图片数组
	private static int[] icons = { R.drawable.widget09, 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;
	}
//返回id
	public Object getItem(int position) {
		
		return position;
	}
//返回id
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
//得到每个gridview的控件.
	public View getView(int position, View convertView, ViewGroup parent) {
		// getview的方法被调用了多少次?9
		// 先预先显示一下,再正式显示.
		// gridview 控件bug 
		// won't fix 
		// 使用静态的变量引用 减少内存中申请的引用的个数 
		
		Log.i(TAG,"getview "+ position);
		//得到view对象.
		View view = inflater.inflate(R.layout.mainscreen_item, null);
		//得到view里面的各个小控件.底层是在栈内存中申请一块空间.
		//用来存放imageview变量.
		//如果getview被调用了16次.就调了iv_icon引用16次.
		//所以要把空间的引用设为静态,就只有一个实例了.
		//使用static减少引用次数.
		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;
	}

}




你可能感兴趣的:(手机卫士知识点二----主界面 gridview.)