导航条的设计与实现
2015-05-31 23:44
197 查看
要实现的界面如下:
需要用到的知识点是ListView组件
下面将介绍自定义ListView组件的方法:
1.在要显示列表的界面布局文件中添加listview组件
2.自定义显示列表项的布局文件
3.定义数据适配器
下面将以案例说明:
1.在要显示列表的界面布局文件中添加listview组件
也就是主界面的布局文件:
3.定义数据适配器
MainActivity实现代码:
需要用到的知识点是ListView组件
下面将介绍自定义ListView组件的方法:
1.在要显示列表的界面布局文件中添加listview组件
2.自定义显示列表项的布局文件
3.定义数据适配器
下面将以案例说明:
1.在要显示列表的界面布局文件中添加listview组件
也就是主界面的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/myList" android:layout_width="fill_parent" android:layout_height="wrap_content" android:dividerHeight="2px" android:layout_marginLeft="4px"/> <!-- 距离左边4像素,android:dividerHeight="2px"表示选项间隔2像素 --> </LinearLayout>2.自定义显示列表项的布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ImageView android:id="@+id/myImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="3px" android:layout_marginTop="3px" /> <TextView android:id="@+id/nameTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="50px" android:textSize="24sp" /> </LinearLayout>
3.定义数据适配器
MainActivity实现代码:
package cn.edu.nnutc; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private ListView listView; private String[] name={"百 度","新 浪","搜 狐","腾 讯","谷 歌"}; private int[] pictrue={R.drawable.baidu,R.drawable.sina,R.drawable.sohu, R.drawable.tencent,R.drawable.google}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.myList);
//定义适配器数据 List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>(); // 定义List,list中存储的是HashMap类型的元素 // 为每个HashMap添加具体的内容 for(int i=0;i<5;i++){ HashMap<String, Object> map = new HashMap<String, Object>(); map.put("picture",pictrue[i] ); map.put("name", name[i]); list.add(map); } /*声明SimpleAdapter, * 第一个参数是Activity上下文; * 第二个参数是数据源; * 第三个参数是每一行的布局资源文件, * 第四个参数是HashMap中的key, * 第五个参数是guidelist.xml文件中组件的id。*/ SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.guidelist, new String[] { "picture", "name" }, new int[] { R.id.myImageView, R.id.nameTextView }); listView.setAdapter(adapter);// 将simpleAdapter绑定到listview上 listView.setOnItemClickListener(new myListener());// 对ListView的item实现点击事件的监听 } class myListener implements OnItemClickListener { private Intent intent; @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { switch (arg2) { case 0: intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")); startActivity(intent); break; case 1: intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sina.com")); startActivity(intent); break; case 2: intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sohu.com")); startActivity(intent); break; case 3: intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.qq.com")); startActivity(intent); break; case 4: intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com.hk")); startActivity(intent); break; } } } }
相关文章推荐
- 深入理解java多线程中的join()
- 用文件的读写和string作为临时存储区复制图片文件
- 关于如何修改weblogic用户密码的问题
- php中一个完整表单处理实现代码
- SRS文档
- 数据库设计(2)_逻辑结构设计
- 如何编译openwrt
- 浅谈委托和事件(一)
- 浮动与定位
- HDU 2047
- Bitnami WordPress如何让默认URL指向WordPress目录?
- iOS - AFN - 错误码解决办法(-1016)
- 圆形链接
- Bitnami WordPress如何让默认URL指向WordPress目录?
- Bitnami WordPress如何让默认URL指向WordPress目录?
- 一步一步源码编译最新版LAMP平台(三)
- 架构和算法
- oracl sql 笔记【未整理】
- 用例图
- 数据库设计(1)_概念结构设计