标签:grideview
1,xml布局
<?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:orientation="vertical" > <GridView android:id="@+id/gv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="3"> </GridView> </LinearLayout> |
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:id="@+id/iv_icon" android:layout_marginTop="10dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/addinaccount"/> <TextView android:id="@+id/tv_newIncom" android:layout_width="wrap_content" android:textSize="20dip" android:layout_below="@+id/iv_icon" android:layout_height="wrap_content" android:text="@string/newIncom"/> </RelativeLayout> |
3,activity代码
publicclass MainActivity extends Activity {
private GridView gv; privateint[] imgs=newint[]{ 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 protectedvoid 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);
} privateclass MyAdapter extends BaseAdapter{
@Override publicint getCount() { // TODO Auto-generated method stub returnimgs.length; }
@Override public Object getItem(int position) { // TODO Auto-generated method stub returnnull; }
@Override publiclong getItemId(int position) { // TODO Auto-generated method stub return 0; }
@Override public View getView(int position, View convertView, ViewGroupparent) {
//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; } } } |
本文出自 “InProvence” 博客,谢绝转载!
标签:grideview
原文地址:http://9882931.blog.51cto.com/9872931/1610919