Android Spinner实现下拉框几种方式及配合BaseAdapter实现自定义布局

本文以Activity中显示XML布局里的Spinner控件为基础实现下拉框。介绍了常规方式,即通过XML布局文件提供静态数据;还讲解了和ArrayAdapter、BaseAdapter配合使用的方法。此外,详细说明了Spinner在代码和XML布局文件中可设置的属性。

目录

前言

一,常规方式

二,和ArrayAdapter配合

三,和BaseAdapter配合使用

Spinner属性


前言

本文以一个Activity中显示XML布局中引入的Spinner控件为基础,实现下拉框;

一,常规方式

这里的常规使用在Xml布局文件中为Spanner提供静态数据;

引入Spinner控件的Activity布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一种用法:"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/data"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        android:prompt="@string/select_info"/>


</androidx.constraintlayout.widget.ConstraintLayout>

strings.xml中定义department数组的数据 

<string name="select_info">请选择</string>
<string-array name="data" >
        <item>Android</item>
        <item>Java </item>
        <item>IOS </item>
        <item>H5 </item>
        <item>python </item>
 </string-array>

上面用到Spinner属性

android:entries="@array/department"  //为Spinner提供下拉显示的数据

android:prompt   //设置提示信息类似于标题,如果没有设置默认显示第一个item的数据;

注意:prompt属性只在dialog状态才有用,Spinner有两种状态:dropdown(默认)和dialog,给Spinner设置状态可以通过spinnerMode属性设置;prompt属性值不能直接输入字符串,必须使用在strings.xml资源文件种自定义的属性,不然会报错,AAPT检查不通过;

android:spinnerMode="dialog"

设置spinner状态还可以通过style设置

//dialog状态 style="@android:style/Widget.Spinner"

//dropdown状态 style="@android:style/Widget.Spinner.DropDown"

直接运行就可以显示下拉框了

二,和ArrayAdapter配合

通过ArrayAdapter适配器为Spinner提供数据,这里要用到Java代码设置数据,不像第一种方式完全都是xml提供并设置数据;

xml布局(熟悉了第一种使用方式这里就不作过多的解释了)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
   
评论 12
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ang_qq_252390816

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值