手机卫士主界面代码

MainActivity:

package com.example.app04_gridview;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
private GridView gv_main;
private MainAdapter adapter;
String[]names = new String[]{"应用1","应用2","应用3","应用4","应用5","应用6","应用7","应用8","应用9"};
int[] icons = new int[]{R.drawable.widget01,R.drawable.widget02,R.drawable.widget03,R.drawable.widget04,R.drawable.widget05,R.drawable.widget06,R.drawable.widget07,R.drawable.widget08,R.drawable.widget09};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv_main=(GridView) findViewById(R.id.gv_main);
adapter=new MainAdapter(MainActivity.this,names,icons);
gv_main.setAdapter(adapter);
//给GridView设置Item的点击监听
gv_main.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//得到当前点击的名称
String name=names[position];
//提示
Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
}

});
}

}

MainAdapter:

package com.example.app04_gridview;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MainAdapter extends BaseAdapter{
private Context context;
private String[] names;
private int[] icons;
public MainAdapter(Context context,String[] names, int[] icons) {
this.context=context;
this.names=names;
this.icons=icons;
}
@Override
public int getCount() {

return names.length;
}


@Override
public Object getItem(int position) {

return names[position];
}
@Override
public long getItemId(int position) {

return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder= null;
if (convertView==null) {
// Log.e("TAG","");                //   日志
convertView = View.inflate(context,R.layout.item_main,null);
holder=new ViewHolder();

holder.imageView=(ImageView) convertView.findViewById(R.id.iv_item_icon);
holder.textView=(TextView) convertView.findViewById(R.id.tv_item_name);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
holder.imageView.setImageResource(icons[position]);
holder.textView.setText(names[position]);
return convertView;
}
class ViewHolder{
ImageView imageView;
TextView textView;
}
}

activity_main布局:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gv_main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:numColumns="3"
    android:verticalSpacing="10dp">

</GridView>

item_main布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:orientation="vertical"
    android:gravity="center"
     >
    <ImageView 
        android:id="@+id/iv_item_icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher"/>
    <TextView 
        android:id="@+id/tv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="名称"
        android:textSize="16sp"
        />

</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值