标签:

android

listview

it

分类: android学习笔记
先看ListView的api文档的说明
A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.
ListView是一个垂直显示选项的滚动列表,它的数据来自ListAdapter。注意它是可以多选,多显示的。

再看Spinner的说明
A view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view.
Spinner只显示并只能选择一个选项的,它的数据来自Adapter。

就是说两者都要通过Adapter来获得数据并显出来。做法都差不多,看如下的代码,可以生成一个下拉框。

public class Select extends Spinner {

ArrayAdapter arr; // 存放数据的数组Adapter,是impl自Adapter的一种

public Select(Context context) {
super(context);
// TODO Auto-generated constructor stub

String[] strs = new String[] {
context.getString(R.string.easy),
context.getString(R.string.normal),
context.getString(R.string.hard) };

// 如果Spinner,ListView中放的内容 是TextView的,必须在res/layout中添加一个xml,
// 且该xml中的内容只能是TextView的xml,以该xml中的TextView做模生成item
arr = new ArrayAdapter(context, R.layout.list_item, strs);
this.setAdapter( arr );
// 通过这样,可以在Spinner中显示3个TextView类型的选项。
}

}

list_item.xml, 该xml中的textView将作为模板使用,可以在这里指定文字的颜色,大小等。
<?xml version="1.0" encoding="UTF-8"?>
<TextView ...>
</TextView>

更多相关文章

  1. android(11)_文件操作读取模式
  2. Android如何把SearchManager和User Dictionary联系在一起
  3. android Activity 清空焦点
  4. Android(安卓)ListView 下拉刷新 上拉更多[实例]
  5. Android(安卓)textview获取选中的内容
  6. android 图片处理大全
  7. Android(安卓)ExpandableListActivity 学习笔记
  8. Android(安卓)sqlit java层源码分析
  9. Android用surface直接显示yuv数据(三)

随机推荐

  1. Android源码获取方法
  2. Handler
  3. android 下载网络pdf文件后并显示
  4. Android进程系列第六篇---LowmemoryKille
  5. Android(安卓)基础
  6. Android系统移植与调试之------->如何修
  7. adb shell 命令详解
  8. 一看就懂的Android(安卓)APP开发入门教程
  9. RN集成自签名https及双向认证-android(2)
  10. Android中程序与Service交互的方式——总