2.1xml布局
<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" > <GridView android:id="@+id/gv" android:numColumns="3" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout> |
2.2 加载布局 xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_marginTop="10dip" android:id="@+id/iv_icon" android:layout_height="70dip" android:layout_width="70dip" android:src="@drawable/addinaccount" /> <TextView android:layout_below="@+id/iv_icon" android:id="@+id/tv_desc" android:layout_marginTop="5dip" android:layout_height="wrap_content" android:layout_width="70dip" android:text="个人收支" android:textSize="16sp" /> </RelativeLayout> |
2.3activity代码
public class MainActivity extends Activity { private GridView gv; private int[] imgs=new int[]{ R.drawable.addinaccount, R.drawable.addoutaccount, R.drawable.inaccountinfo, R.drawable.outaccountinfo, R.drawable.accountflag, R.drawable.showinfo, R.drawable.appicon, R.drawable.manger, R.drawable.exit }; private String[] desc=new String[]{ "个人收入","个人支出","收入信息","支出信息","数据管理","系统设置","收支便签","家庭成员","应用退出" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1,找到控件 gv = (GridView) findViewById(R.id.gv); //2,自定义适配器 MyAdapter adapter=new MyAdapter(); //3,加载 gv.setAdapter(adapter); } private class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return imgs.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { //1,找到布局 View view=View.inflate(getApplicationContext(), R.layout.gv_item, null); //2,找到布局要操作的控件 ImageView iv= (ImageView) view.findViewById(R.id.iv_icon); TextView tv=(TextView) view.findViewById(R.id.tv_desc); //3,加载数据 iv.setImageResource(imgs[position]); tv.setText(desc[position]); return view; } } } |
只写了部分,待日后再补充