Android开发学习笔记-GridView的动态显示
1、添加GridItem布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/iv_image" android:layout_width="wrap_content" android:layout_height="60dp" android:src="@drawable/app" /> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置中心" /> </LinearLayout>
2、主界面添加GridView
<GridView android:id="@+id/grid_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numColumns="3" > </GridView>
3、添加自定义适配器类,并对自定义griditem布局文件中的控件进行设置值
protected class MyGridAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return titles.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) { // TODO Auto-generated method stub View view = View.inflate(MainActivity.this, R.layout.list_item_main, null); TextView title = (TextView) view.findViewById(R.id.tv_title); title.setText(titles[position]); return view; }
4、设置主界面中的GridItem
private GridView grid_main; private MyGridAdapter adapter; private static String []titles = {"设置中心1","设置中心2","设置中心3","设置中心4","设置中心5","设置中心6","设置中心7","设置中心8","设置中心9"}; .... grid_main = (GridView) findViewById(R.id.grid_main); adapter = new MyGridAdapter(); grid_main.setAdapter(adapter);
5、添加item点击事件
grid_main.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub ConsoleItemClick(position); } });
6、显示
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。