1.NiceSpinner下拉框控件介绍

Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Android原生提供的下拉框Spinner所提供的设计样式,而改用自定制或者第三方设计的下拉框Spinner。NiceSpinner是一个第三方开源的下拉框Spinner。

2.使用步骤

(1)build.gradle(project)中一段代码替换为如下内容:(android studio工程的标配)

buildscript {

    repositories {
google()
maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url "https://jitpack.io" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
} allprojects {
repositories {
maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
google()
maven { url "https://jitpack.io" }
}
}

(2)build.gradle(app)中添加依赖

dependencies {
implementation 'com.github.arcadefire:nice-spinner:1.4'
}

3.NiceSpinner下拉框控件的属性分析

You can add attributes to customize the view. Available attributes:

用户可以在xml文件布局中添加以下表格中的属性到NiceSpinner控件中,对NiceSpinner进行设置。

arrowTint color 设置下拉箭头上的颜色
hideArrow boolean
设置是显示还是隐藏下拉箭头
arrowDrawable reference set the drawable of the drop-down arrow
textTint color 设置文本颜色
dropDownListPaddingBottom dimension 设置下拉列表的底部填充(即设置下拉框控件的高度)

4.使用案例

(1)xml文件页面布局

<1>主界面页面布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity"> <!--app:arrowTint="@color/red" 设置下拉箭头的颜色-->
<!--android:backgroundTint="@color/gray" 设置下拉框整体的颜色-->
<!--app:textTint="@color/blue" 设置下拉框字体的颜色-->
<!--android:backgroundTint="@color/pink" 设置整个空间的背景颜色-->
<org.angmarch.views.NiceSpinner
android:id="@+id/nice_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:arrowTint="@color/red"
app:textTint="@color/blue"
android:layout_margin="16dp"/> <Button
android:id="@+id/bt_getvalue"
android:text="获取选中值"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout>

<2>shape绘制NiceSpinner控件的背景图

平常在开发当中,通常会遇到这样的情况,就是会给控件增加一个背景,比如button,textview等!可以说shape就是一个形状定义工具。是xml绘图当中非常重要的一个工具。

使用shape一般是用来定义形状的,可以在xml上绘图,意思就是shape的表现形式就是一个xml文件,这个xml文件一般是放在drawable文件目录下,然后可以直接引用作为控件的背景。

shape_nicespinner.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- view背景色 -->
<solid android:color="#52a2e2" />
<!-- 边框颜色 宽度 -->
<stroke
android:width="1dip"
android:color="#52a2e2" />
<!-- 边框圆角 -->
<corners
android:bottomRightRadius="6dp"
android:topRightRadius="6dp"
android:bottomLeftRadius="6dp"
android:topLeftRadius="6dp"/>
</shape>

shape_button_bg_normal.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- view背景色 -->
<solid android:color="@color/pink" />
<!-- 边框颜色 宽度 -->
<stroke
android:width="1dip"
android:color="@color/pink" />
<!-- 边框圆角 -->
<corners
android:bottomRightRadius="15dp"
android:topRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"/>
</shape>

shape_button_bg_press.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- view背景色 -->
<solid android:color="@color/blue" />
<!-- 边框颜色 宽度 -->
<stroke
android:width="1dip"
android:color="@color/blue" />
<!-- 边框圆角 -->
<corners
android:bottomRightRadius="15dp"
android:topRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"/>
</shape>

<3>图片选择器selector应用

selector_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--选中用蓝色的图-->
<item android:state_pressed="true" android:drawable="@drawable/shape_button_bg_press"></item>
<!--未选中用粉色的图-->
<item android:drawable="@drawable/shape_button_bg_normal"></item>
</selector>

(2)java后台

package com.example.administrator.test64nicespinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; import org.angmarch.views.NiceSpinner; import java.util.Arrays;
import java.util.LinkedList;
import java.util.List; public class MainActivity extends AppCompatActivity { Button bt_getvalue;
NiceSpinner niceSpinner; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
} private void initUI() {
bt_getvalue = findViewById(R.id.bt_getvalue);
bt_getvalue.setBackgroundResource(R.drawable.selector_button); //给button设置自定义样式
bt_getvalue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),niceSpinner.getText(),Toast.LENGTH_SHORT).show();
}
});
niceSpinner = findViewById(R.id.nice_spinner);
List<String> dataset = new LinkedList<>(Arrays.asList("One", "Two", "Three", "Four", "Five"));
niceSpinner.attachDataSource(dataset); //设置下拉框要显示的数据集合
niceSpinner.setBackgroundResource(R.drawable.shape_nicespinner); //设置控件的形状和背景
}
}

5.效果图

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

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

     Android第三方开源下拉框:NiceSpinner Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Andro ...

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

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

  3. android 开发-spinner下拉框控件的实现

    Android提供实现下拉框功能的非常实用的控件Spinner. spinner控件需要向xml资源文件中添加spinner标签,如下: <Spinner android:id="@+ ...

  4. android+myeclipse+mysql下拉框数据绑定

    原创作品,允许转载,转载时请务必声明作者信息和本声明.http://www.cnblogs.com/zhu520/p/8027036.html 本人小白,那个大神看到有问题可指出,谢谢.... 一:我 ...

  5. Android 控件背景选择图片还是drawable XML资源

    决定一个控件应该是否用Drawable XML渲染,应考虑以下几个因素: * App是否要支持多分辨率: * App是否有瘦身的需要: * 图案是否足够简单: * 图案需要自由缩放: * 设计开发工作 ...

  6. Android 自定义spinner下拉框实现的实现

    请支持原创:http://blog.csdn.NET/geniuseoe2012/article/details/8723702 说到Android下拉框spineer,框架中虽有现成的控件,但实际效 ...

  7. 快速设计ComboBox下拉框

    传统软件项目开发时,需要每个控件一个一个的来设计,同时需要在页面功能中对每个控件的属性进行判定处理,尤其是页面风格布局样式需要花去一大半的时间,并且后续要想修改是非常麻烦繁琐,这样就导致设计完成一个功 ...

  8. android下拉框

    XML: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...

  9. android+myeclipse+mysql自定义控件下拉框的数据绑定

    原创作品,允许转载,转载时请务必声明作者信息和本声明.http://www.cnblogs.com/zhu520/p/8031936.html 本人小白,那个大神看到有问题可指出,谢谢.... 这个是 ...

随机推荐

  1. Pytest权威教程22-优质集成实践

    目录 优质集成实践 使用pip安装包 Python测试发现的约定 选择测试布局结构/导入规则 在应用程序代码外测试 测试作为应用程序代码的一部分 tox 返回: Pytest权威教程 优质集成实践 使 ...

  2. 记一次vue+vuex+vue-router+axios+elementUI开发(一)

    记录自己的vue开发之旅,方便之后查询 一.开发环境 1.安装node.js  自带npm https://nodejs.org/en/ 2. 全局安装vue-cli脚手架 npm install v ...

  3. [学习笔记] 二叉查找树/BST

    平衡树前传之BST 二叉查找树(\(BST\)),是一个类似于堆的数据结构, 并且,它也是平衡树的基础. 因此,让我们来了解一下二叉查找树吧. (其实本篇是作为放在平衡树前的前置知识的,但为了避免重复 ...

  4. 单一职责原则(SRP)

    内聚性:一个模块的组成元素之间的功能相关性.就一个类而言,应该仅有一个引起它变化的原因.当需求变化时,该变化会反映为类的职责的变化,如果一个类承担了多于一个的职责,那么引起它变化的原因就会有多个.如果 ...

  5. Eclipse 搭建Struts2

    Eclipse版本 Mars Release (4.5.0) Struts版本 struts-2.5.20 下载地址:https://struts.apache.org/download.cgi#st ...

  6. LF: 换行,U+000A VT: 垂直定位,U+000B FF: 换页符,U+000C CR: 回车符,U+000D CR+LF:CR(U+000D)后跟LF(U+000A) NEL: 下一行,U+0085 LS: 分行,U+2028 PS: 分段,U+2029

    https://zh.wikipedia.org/wiki/換行 换行(英语:newline.line ending.end-of-line (EOL).line Feed (LF).line bre ...

  7. MyBatis:MyBatis操作MySQL存储过程

    一 . 数据库中创建存储过程,并查看创建结果 1.创建存储过程 DROP procedure IF EXISTS net_procedure_request; DELIMITER $$ )) BEGI ...

  8. android ------ 实现高德定位并获取相应信息 ( 最新版高德SDK 和 Android SDK版本)

    Android开发项目时常常会遇到定位这个功能, 很久以前写过一篇了,官方也更新了一些东西,我也更新下 以前使用的是jar包 导入来实现高德定位 老版本 链接:https://www.cnblogs. ...

  9. 使用 CircleCI 2.0 进行持续集成/持续部署

    使用 CircleCI 2.0 进行持续集成/持续部署 - 简书https://www.jianshu.com/p/36af6af74dfc Signup - CircleCIhttps://circ ...

  10. Linux 打开文件数

    linux设置最大打开文件数 - daiyudong2020的博客 - CSDN博客 https://blog.csdn.net/daiyudong2020/article/details/77828 ...