1. 实现最简单的spinner

xml文件,有一个TextView,一个Spinner:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" /> <Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp" /> </RelativeLayout>

.java文件

public class MainActivity extends ActionBarActivity {
private static final String[] name={"刘备","关羽","张飞","曹操","小乔"};
private TextView text ;
private Spinner spinner;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.textView);
spinner = (Spinner) findViewById(R.id.spinner); //将可选内容与ArrayAdapter连接起来,simple_spinner_item是android系统自带样式
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,name);
//设置下拉列表的风格,simple_spinner_dropdown_item是android系统自带的样式,等会自己定义改动
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将adapter 加入到spinner中
spinner.setAdapter(adapter);
//加入事件Spinner事件监听
spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
} //使用数组形式操作
class SpinnerSelectedListener implements AdapterView.OnItemSelectedListener { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
text.setText("我的名字是:"+name[arg2]);
} public void onNothingSelected(AdapterView<?> arg0) {
}
}

执行效果:

—————————————————————

使用xml文件作为数据源创建adapter:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="songs">
<item>没有人</item>
<item>我的快乐时代</item>
<item>黄金时代</item>
<item>习惯失恋</item>
<item>你来自哪颗星</item>
</string-array>
</resources>

.java文件:

public class SpinnerActivity extends Activity {  

    private TextView text;
private Spinner spinner;
private ArrayAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner); spinner = (Spinner) findViewById(R.id.spinner);
text = (TextView) findViewById(R.id.textView); //将可选内容与ArrayAdapter连接起来
adapter = ArrayAdapter.createFromResource(this, R.array.songs, android.R.layout.simple_spinner_item); //设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //将adapter2 加入到spinner中
spinner.setAdapter(adapter); //加入事件Spinner事件监听
spinner.setOnItemSelectedListener(new SpinnerXMLSelectedListener()); } //使用XML形式操作
class SpinnerXMLSelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3) {
text.setText("你使用什么样的手机:"+adapter.getItem(position));
} public void onNothingSelected(AdapterView<? > arg0) { } }
}

spinner有三个属性能够记一下:

 android:spinnerMode="dropdown"
android:dropDownVerticalOffset="-50dp"
android:dropDownHorizontalOffset="20dp"
android:popupBackground="#f0000000"

spinnerMode=dropdown时,为下拉模式

spinnerMode=dialog时,会在界面中间弹出

android:popupBackground=”#f0000000”,能够去除spinner的默认黑边

dropDownVerticalOffset和dropDownHorizontalOffset都是改变下拉框位置的

2.自己定义spinner样式

改变字体颜色、大小和背景:

新建一个xml布局文件,命名为spinner_item.xml:

<?xml version="1.0" encoding="utf-8"?>

    <TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:textColor="#f77718"
android:gravity="left"
android:textSize="15sp"
android:padding="10dp"
android:singleLine="true"
android:text="New Text"
android:id="@+id/textView32" />

再创建一个下拉框样式布局的xml文件。命名为dropdown_stytle.xml:

<?

xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:padding="10dp"
android:singleLine="true"
android:textSize="15sp"
android:textColor="#f77718"
android:gravity="left"
android:background="#aa33ac"
android:id="@+id/textView3333" />

改动之前.java中的



为:

    adapter = new ArrayAdapter<String>(this,R.layout.spinner_item,name);

    adapter.setDropDownViewResource(R.layout.dropdown_style);

假设下拉框有黑边,能够在spinner中加上属性android:popupBackground=”#f0000000”,能够去除spinner的默认黑边,that’s all~

最后的效果图:

android实现下拉框(spinner),自己定义大小颜色背景位置,去掉默认样式黑边的更多相关文章

  1. Android 之 下拉框(Spinner)的使用

    下拉列表 Spinner. Spinner的使用,可以极大提高用户的体验性.当需要用户选择的时候,可以提供一个下拉列表将所有可选的项列出来.供用户选择. Demo如下,可以留作参考 一.使用数组作为数 ...

  2. 【转】Android 之 下拉框(Spinner)的使用

    原文网址:http://imshare.iteye.com/blog/770950 下拉列表 Spinner. Spinner的使用,可以极大提高用户的体验性.当需要用户选择的时候,可以提供一个下拉列 ...

  3. Android 之 下拉框(Spinner)的使用-转

    下拉列表 Spinner. Spinner的使用,可以极大提高用户的体验性.当需要用户选择的时候,可以提供一个下拉列表将所有可选的项列出来.供用户选择. Demo如下,可以留作参考 一.使用数组作为数 ...

  4. Android零基础入门第46节:下拉框Spinner

    原文:Android零基础入门第46节:下拉框Spinner 上一期学习了GridView的使用,你已经掌握了吗?本期一起来学习Spinner的使用. 一.认识Spinner Spinner其实就是一 ...

  5. 下拉框spinner

    repositories { flatDir { dirs 'libs' //就是你放aar的目录地址 maven { url "https://jitpack.io" } }}d ...

  6. Android实现三级联动下拉框 下拉列表spinner

    Android实现(省.市.县)三级联动下拉框 下拉列表spinner 转载请注明出处: http://www.goteny.com/articles/2013/11/46.html http://w ...

  7. Android实现三级联动下拉框下拉列表spinner

    原文出处:http://www.cnblogs.com/zjjne/archive/2013/10/03/3350107.html 主要实现办法:动态加载各级下拉值的适配器 在监听本级下拉框,当本级下 ...

  8. 030 Android 第三方开源下拉框:NiceSpinner的使用+自定义Button样式+shape绘制控件背景图+图片选择器(selector)

    1.NiceSpinner下拉框控件介绍 Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Android原生提供的下拉框 ...

  9. Android 第三方开源下拉框:NiceSpinner

    Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Android原生提供的下拉框Spinner所提供的设计样式,而改用自定 ...

随机推荐

  1. luci框架-LUA的一个web框架使用

    转自:http://blog.csdn.net/initphp/article/details/17527639 LUCI 这个在百度上搜索除了一篇我的百度文库 luci 的介绍文章之外,前三页都是些 ...

  2. Hive:用Java代码通过JDBC连接Hiveserver

    参考https://www.iteblog.com/archives/846.html 1.hive依赖hadoop,将hdfs当作文件存储介质,那是否意味着hive需要知道namenode的地址? ...

  3. C#.net开发 List与DataTable相互转换 【转】

    http://blog.csdn.net/shuizhaoshui/article/details/51425527 在.NET开发中,操作关系型数据库提取数据经常用到DataTable.ASP.NE ...

  4. unity color space 选取

    https://unity3d.com/cn/learn/tutorials/topics/graphics/choosing-color-space https://docs.unity3d.com ...

  5. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

  6. Cocos2d-x3.0游戏实例之《别救我》第七篇——物理世界的碰撞检測

    事实上我也非常吃惊-居然写到第七篇了,我估计也就是四篇的内容,感觉非常奇妙,我也不会非常唠叨什么吖);    // 0001 );   // 0001 ); // 0001 这样我们才干监听到它们的碰 ...

  7. Java中PriorityQueue详解

    Java中PriorityQueue通过二叉小顶堆实现,可以用一棵完全二叉树表示.本文从Queue接口函数出发,结合生动的图解,深入浅出地分析PriorityQueue每个操作的具体过程和时间复杂度, ...

  8. 转: iOS崩溃堆栈符号表使用与用途

    转:http://bugly.qq.com/blog/?p=119 iOS崩溃堆栈符号化,定位问题分分钟搞定! 2015.3.16 腾讯Bugly 微信分享   最近一段时间,在跟开发者沟通过程中,萝 ...

  9. powerdesigner里建物理模型图时choose DBMS为空怎么办?

    RT 出现如下对话框,是因为需要“DBMS”的规则文件夹 点击下图文件图标,浏览,找到安装目录里面PowerDesigner 15\Resource Files\DBMS,就可以了. 在此记录一下,希 ...

  10. PHP基础知识(一)

    The Basics Comparison operators Comparison operators are an often overlooked aspect of PHP, which ca ...