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; } } } |
只寫了部分,待日後再補充