Spinner的样式大致简介
Spinner
Spinner 是一个列表选择框,会在用户选择后,展示一个列表供用户进行选择。Spinner是ViewGroup的间接子类,它和其他的Android控件一样,数据需要使用Adapter进行封装。
下面介绍一下Spinner的常用XML属性,Android也为其属性提供了相应的getter、setter方法:
- android:spinnerMode:列表显示的模式,有两个选择,为弹出列表(dialog)以及下拉列表(dropdown),如果不特别设置,为下拉列表。。
- android:entries:使用<string-array.../>资源配置数据源。
- android:prompt:对当前下拉列表设置标题,仅在dialog模式下有效。传递一个“@string/name”资源,需要在需要在资源文件中定义<string.../>。
作为一个列表选择控件,Spinner具有一些选中选项可以触发的事件,但它本身没有定义这些事件,均继承自间接父类 AdapterView 。Spinner支持的几个常用事件有以下几个:
- AdapterView.OnItemCLickListener:列表项被点击时触发。
- AdapterView.OnItemLongClickListener:列表项被长按时触发。
- AdapterView.OnItemSelectedListener:列表项被选择时触发。
PS:因为适配器可以设置各种不同的样式,有选择、单选、多选,所以OnItemCLickListener和OnItemSelectedListener是适用于不同场景的。
Spinner的数据绑定
对于Spinner展示的数据源,一般使用两种方式设定数据:
- 通过XML资源文件设置,这种方式比较死板,但是如果仅仅需要展示固定的、简单的数据,这种方式还是可以考虑的,比较直观。
- 使用Adapter接口设置,这是最常见的方式,动态、灵活,可以设定各种样式以及数据来源。
先来讲讲通过XML资源文件设置Spinner数据的方式,首先需要在/res/values目录下新建XML格式的资源文件,名字不重要,但是一般会使用strings.xml。在其中的<resourse.../>标签下,定义<string-array.../>标签,通过它中的<item.../>标签来设置选择数据。
XML文件结构:
<resource>
<string-array name="arrayname">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</string-array>
<resource>
通过适配器Adapter可以设定比较复杂的展示效果,一般项目中比较常用的也是这种方式。但是如果对于动态的、简单的数据,可以使用ArrayAdapter对象来设置适配器,关于ArrayAdapter类的介绍,在我的另外一篇博客中有介绍,不了解的朋友可以先看看: Android--UI之AutoCompleteTextView 。
下面通过一个示例,讲解一下上面说的属性、事件,以及使用ArrayAdapter和XML资源文件设定简单数据,代码中注释已经说的很清楚了,这里就不再累述了。
布局代码:
- 1 <?xml version="1.0" encoding="utf-8"?>
- 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 3 android:layout_width="match_parent"
- 4 android:layout_height="match_parent"
- 5 android:orientation="vertical" >
- 6
- 7 <TextView
- 8 android:layout_width="wrap_content"
- 9 android:layout_height="wrap_content"
- 10 android:text="弹出的Spinner" />
- 11
- 12 <Spinner
- 13 android:id="@+id/spinnerBase"
- 14 android:layout_width="match_parent"
- 15 android:layout_height="wrap_content"
- 16 android:spinnerMode="dialog" />
- 17
- 18 <TextView
- 19 android:layout_width="wrap_content"
- 20 android:layout_height="wrap_content"
- 21 android:text="下拉的Spinner(默认)" />
- 22
- 23 <Spinner
- 24 android:id="@+id/spinnerBase1"
- 25 android:layout_width="match_parent"
- 26 android:layout_height="wrap_content"
- 27 android:spinnerMode="dropdown" />
- 28
- 29 <TextView
- 30 android:layout_width="wrap_content"
- 31 android:layout_height="wrap_content"
- 32 android:text="entries绑定数据源" />
- 33
- 34 <Spinner
- 35 android:id="@+id/spinnerBase2"
- 36 android:layout_width="match_parent"
- 37 android:layout_height="wrap_content"
- 38 android:entries="@array/beijing" />
- 39
- 40 <TextView
- 41 android:layout_width="wrap_content"
- 42 android:layout_height="wrap_content"
- 43 android:text="弹出带标题的Dialog,并且使用entries绑定数据源" />
- 44
- 45 <Spinner
- 46 android:id="@+id/spinnerBase3"
- 47 android:layout_width="match_parent"
- 48 android:layout_height="wrap_content"
- 49 android:entries="@array/beijing"
- 50 android:prompt="@string/beij_prompt"
- 51 android:spinnerMode="dialog" />
- 52
- 53 </LinearLayout>
实现代码:
- 1 package com.bgxt.datatimepickerdemo;
- 2
- 3 import java.util.ArrayList;
- 4 import java.util.List;
- 5
- 6 import android.app.Activity;
- 7 import android.os.Bundle;
- 8 import android.view.View;
- 9 import android.widget.AdapterView;
- 10 import android.widget.AdapterView.OnItemSelectedListener;
- 11 import android.widget.ArrayAdapter;
- 12 import android.widget.Spinner;
- 13 import android.widget.Toast;
- 14
- 15 public class SpinnerBaseActivity extends Activity {
- 16 private Spinner spinner1, spinner2;
- 17
- 18 @Override
- 19 protected void onCreate(Bundle savedInstanceState) {
- 20 super.onCreate(savedInstanceState);
- 21 setContentView(R.layout.activity_spinnerbase);
- 22
- 23 spinner1 = (Spinner) findViewById(R.id.spinnerBase);
- 24 spinner2 = (Spinner) findViewById(R.id.spinnerBase1);
- 25 // 声明一个ArrayAdapter用于存放简单数据
- 26 ArrayAdapter<String> adapter = new ArrayAdapter<String>(
- 27 SpinnerBaseActivity.this, android.R.layout.simple_spinner_item,
- 28 getData());
- 29 // 把定义好的Adapter设定到spinner中
- 30 spinner1.setAdapter(adapter);
- 31 spinner2.setAdapter(adapter);
- 32 // 为第一个Spinner设定选中事件
- 33 spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
- 34
- 35 @Override
- 36 public void onItemSelected(AdapterView<?> parent, View view,
- 37 int position, long id) {
- 38 // 在选中之后触发
- 39 Toast.makeText(SpinnerBaseActivity.this,
- 40 parent.getItemAtPosition(position).toString(),
- 41 Toast.LENGTH_SHORT).show();
- 42 }
- 43
- 44 @Override
- 45 public void onNothingSelected(AdapterView<?> parent) {
- 46 // 这个一直没有触发,我也不知道什么时候被触发。
- 47 //在官方的文档上说明,为back的时候触发,但是无效,可能需要特定的场景
- 48 }
- 49 });
- 50
- 51 }
- 52
- 53 private List<String> getData() {
- 54 // 数据源
- 55 List<String> dataList = new ArrayList<String>();
- 56 dataList.add("北京");
- 57 dataList.add("上海");
- 58 dataList.add("南京");
- 59 dataList.add("宜昌");
- 60 return dataList;
- 61 }
- 62
- 63 }
XML资源文件:
- 1 <?xml version="1.0" encoding="utf-8"?>
- 2 <resources>
- 3 <string name="app_name">SpinnerDemo</string>
- 4 <string name="action_settings">Settings</string>
- 5 <string name="hello_world">Hello world!</string>
- 6 <string name="beij_prompt">北京区域</string>
- 7 <string-array name="beijing">
- 8 <item>朝阳区</item>
- 9 <item>海淀区</item>
- 10 <item>房山区</item>
- 11 <item>丰台区</item>
- 12 <item>东城区</item>
- 13 <item>西城区</item>
- 14 </string-array>
- 15 </resources>
效果展示,图片顺序,从上到下:
SimpleAdapter配置Spinner数据
对于一个稍复杂的数据,如果想对其展示,光使用ArrayAdapter是无法满足需求的,现在在另外介绍一个Adapter, SimpleAdapter ,同样继承自Adapter。
SimpleAdapter是一个简单的适配器,映射静态的XML格式的布局文件到视图中。可以指定一个List<Map<P,T>>格式的数据,List中的每一条数据对应一行,而Map中的每一条数据对应数据行的一列。这个数据用来映射到XML定义的布局控件中,对应关系通过构造函数的另外两个参数来指定,现在来介绍一下SimpleAdapter的构造函数。
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
- context:上下文对象,没什么好说的,一般就是当前的Activity。
- data:上面介绍的List<Map<S,T>>类型的数据。
- resource:XML资源的Id,通过R对象选中。
- from:一个String类型数组,每条数据对应data数据中,Map结构定义的Key。
- to:一个int类型数组,对应XML资源中控件的ID,注意顺序必须与from中指定数据的顺序一致。
下面通过一个示例讲解一下SimpleAdapter是如何设置自定义格式数据的。
布局代码:
- 1 <?xml version="1.0" encoding="utf-8"?>
- 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 3 android:layout_width="match_parent"
- 4 android:layout_height="match_parent"
- 5 android:orientation="vertical" >
- 6
- 7 <Spinner android:id="@+id/spinnerAdapter" android:layout_width="match_parent"
- 8 android:layout_height="wrap_content" />
- 9 </LinearLayout>
XML布局资源代码:
- 1 <?xml version="1.0" encoding="utf-8"?>
- 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 3 android:layout_width="match_parent"
- 4 android:layout_height="wrap_content"
- 5 android:orientation="horizontal" >
- 6
- 7 <ImageView
- 8 android:id="@+id/imageview"
- 9 android:layout_width="60dp"
- 10 android:layout_height="60dp"
- 11 android:paddingLeft="10dp"
- 12 android:src="@drawable/ic_launcher" />
- 13
- 14 <TextView
- 15 android:id="@+id/textview"
- 16 android:layout_width="match_parent"
- 17 android:layout_height="wrap_content"
- 18 android:gravity="center_vertical"
- 19 android:paddingLeft="10dp"
- 20 android:textColor="#000"
- 21 android:textSize="16dp" />
- 22
- 23 </LinearLayout>
实现代码:
- 1 package com.bgxt.datatimepickerdemo;
- 2
- 3 import java.util.ArrayList;
- 4 import java.util.HashMap;
- 5 import java.util.List;
- 6 import java.util.Map;
- 7
- 8 import android.app.Activity;
- 9 import android.os.Bundle;
- 10 import android.view.View;
- 11 import android.widget.AdapterView;
- 12
- 13 import android.widget.AdapterView.OnItemSelectedListener;
- 14 import android.widget.SimpleAdapter;
- 15 import android.widget.Spinner;
- 16 import android.widget.Toast;
- 17
- 18 public class SpinnerAdapterActivity extends Activity {
- 19 private Spinner spinner;
- 20
- 21 @Override
- 22 protected void onCreate(Bundle savedInstanceState) {
- 23 // TODO Auto-generated method stub
- 24 super.onCreate(savedInstanceState);
- 25 setContentView(R.layout.activity_spinneradapter);
- 26
- 27 spinner = (Spinner) findViewById(R.id.spinnerAdapter);
- 28 //声明一个SimpleAdapter独享,设置数据与对应关系
- 29 SimpleAdapter simpleAdapter = new SimpleAdapter(
- 30 SpinnerAdapterActivity.this, getData(), R.layout.items,
- 31 new String[] { "ivLogo", "applicationName" }, new int[] {
- 32 R.id.imageview, R.id.textview });
- 33 //绑定Adapter到Spinner中
- 34 spinner.setAdapter(simpleAdapter);
- 35 //Spinner被选中事件绑定。
- 36 spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
- 37
- 38 @Override
- 39 public void onItemSelected(AdapterView<?> parent, View view,
- 40 int position, long id) {
- 41 //parent为一个Map结构的和数据
- 42 Map<String, Object> map = (Map<String, Object>) parent
- 43 .getItemAtPosition(position);
- 44 Toast.makeText(SpinnerAdapterActivity.this,
- 45 map.get("applicationName").toString(),
- 46 Toast.LENGTH_SHORT).show();
- 47 }
- 48
- 49 @Override
- 50 public void onNothingSelected(AdapterView<?> arg0) {
- 51
- 52 }
- 53 });
- 54 }
- 55
- 56 public List<Map<String, Object>> getData() {
- 57 //生成数据源
- 58 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
- 59 //每个Map结构为一条数据,key与Adapter中定义的String数组中定义的一一对应。
- 60 Map<String, Object> map = new HashMap<String, Object>();
- 61 map.put("ivLogo", R.drawable.bmp1);
- 62 map.put("applicationName", "表情1");
- 63 list.add(map);
- 64 Map<String, Object> map2 = new HashMap<String, Object>();
- 65 map2.put("ivLogo", R.drawable.bmp2);
- 66 map2.put("applicationName", "表情2");
- 67 list.add(map2);
- 68 Map<String, Object> map3 = new HashMap<String, Object>();
- 69 map3.put("ivLogo", R.drawable.bmp3);
- 70 map3.put("applicationName", "表情3");
- 71 list.add(map3);
- 72 return list;
- 73 }
- 74 }
效果展示:
Spinner的样式大致简介的更多相关文章
- 【Android 应用开发】 ActionBar 样式详解 -- 样式 主题 简介 Actionbar 的 icon logo 标题 菜单样式修改
作者 : 万境绝尘 (octopus_truth@163.com) 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/3926916 ...
- Android spinner默认样式不支持换行和修改字体样式的解决方法
在spinner中显示的数据过多,需要换行,而Android自身提供的android.R.layout.simple_spinner_dropdown_item样式不支持换行,因此参考android提 ...
- HTML 学习笔记 CSS样式(简介和语法)
CSS概述 CSS指层叠样式表(Cascading Style Sheets) 样式定义如何显示HTML元素 样式通常存储在样式表中 把样式添加到HTML4.0中 是为了解决内容与表现分离的问题 外部 ...
- tomcat——大致简介和执行过程
jsp简介 JSP: JAVA Server Page 使用JAVA语言编写的一种在服务器运行的动态页面 JSP = JAVA + HTML JSP 的执行过程 1: 翻译阶段 把JSP源文件翻译成 ...
- Android Spinner使用简介
Android中使用Spinner作为下拉列表,下面直接看实现方式: (1)使用ArrayAdapter来实现: 实现步骤: 1. 在布局文件中定义Spinner组件: 2. 向Spinner添加需要 ...
- Android开发之自定义Spinner样式的效果实现(源代码实现)
android系统自带的Spinner样式是远远满足不了我们实际开发过程中对Spinner UI风格的要求,因此我们肯定需要为了切合整个应用的风格,修改我们的Spinner样式.系统给我们提供了两种常 ...
- Android开发之自己定义Spinner样式的效果实现(源码实现)
android系统自带的Spinner样式是远远满足不了我们实际开发过程中对Spinner UI风格的要求,因此我们肯定须要为了切合整个应用的风格,改动我们的Spinner样式.系统给我们提供了两种常 ...
- spinner -样式实现
这里主要是在theme中实现spinner的样式,如下 <style name="Theme.Funui" parent="Theme.Holo.Light&qu ...
- Android之Socket通信、List加载更多、Spinner下拉列表
Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户端向服务器发送请求后,服务 ...
随机推荐
- buf.writeUInt16BE()
buf.writeUInt16BE(value, offset[, noAssert]) buf.writeUInt16LE(value, offset[, noAssert]) value {Num ...
- Python学习笔记 (2)变量、常量和数据类型
变量 顾名思义,变量就是一个会变的量,用一个变量名表示,指向内存中一片区域,而指向的区域存的是什么,这个变量就是什么数据类型,和C/C++挺不一样的.变量数据类型可以通过赋值变来变去(这就叫动态语言, ...
- [HDU3586]Information Disturbing(DP + 二分)
传送门 题意:给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超过上限limit,问在保证总费用<=m下的最小的limit 二分答案,再 DP,看看最终结果是 ...
- Codeforces Round #226 (Div. 2) C题
数论好题 题目要求:求给定序列的素因子如果在给定区间内该数字个数加1; 思路:打表时求出包含给素数因子的数的个数,详见代码 1 #include<cstring> +; scan ...
- [bzoj1176]Mokia[CDQ分治]
啃了一天论文,发现CDQ分治的原理其实很简单,大概就是这样的一类分治:将左右区间按一定规律排序后分开处理,递归到底时直接计算答案,对于一个区间,按照第二关键字split成两个区间,先处理左区间,之后因 ...
- update city_demo set city=(select city from city order by rand() limit1);
update city_demo set city=(select city from city order by rand() limit1); 因为使用了rand()函数,所以每一次查询的结果是不 ...
- STM8S PWM 应用 呼吸灯
//主功能接受:使用MCU STM8S105C6 的PWM通道2 PC2 来做呼吸灯 已经验证OK,呵 //呵,这个PWM设置刚開始用还是有点麻烦,由于是自己摸索.花点时间.还是解决了 . //所用子 ...
- jquery-mobile 学习笔记之二(表单创建)
绪上 一.注意事项 1. <form> 元素必须设置 method 和 action 属性 2. 每一个表单元素必须设置唯一的 "id" 属性. 该 id 在网站的页面 ...
- 精确计算java中float和double的精度
[本文相关的代码放在github上.地址为:https://github.com/VigourJiang/StructuredFloat] Java中double类型的格式基本遵循IEEE 754标准 ...
- (WIP) DPDK理论学习(by quqi99)
作者:张华 发表于:2016-04-22版权声明:能够随意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) 组成模 ...