当前位置: 首页 > web >正文

Android 中自定义控件实现 AppCompatSpinner 功能

在为实现沉浸式效果需要隐藏导航栏的应用中,由于采用系统自带的 AppCompatSpinner 控件实现下拉列表选择功能时,底部会出现一个白色导航栏,目前未找到隐藏的办法,故只能完全自定义一个下拉列表选择控件。

1、基本思路

  • 定义一个内容项的布局,包含一个 TextView 和一个 ImageView 。
  • 定义一个包含 RecyclerView 控件的 PopupWindow 下拉窗口。
  • 定义 RecyclerView 的 子项布局和 Adapter 适配器。
  • 定义下拉列表打开和关闭时的动画效果。
  • 自定义一个下拉列表控件。

2、内容项布局文件

  • custom_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"><TextViewandroid:id="@+id/item_text"android:layout_width="match_parent"android:layout_height="match_parent"android:textSize="@dimen/sp_20"android:textColor="@color/white"android:paddingHorizontal="@dimen/dp_20"android:gravity="center_vertical"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:src="@mipmap/weather_sun"app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintRight_toRightOf="parent"android:layout_marginEnd="@dimen/dp_20"tools:ignore="ContentDescription" /></androidx.constraintlayout.widget.ConstraintLayout>

3、下拉列表布局文件

  • custom_spinner_dropdown.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"xmlns:app="http://schemas.android.com/apk/res-auto"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/dropdown_recycler"android:layout_width="match_parent"android:layout_height="@dimen/dp_200"android:scrollbars="vertical"android:fadeScrollbars="false"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout>

4、下拉列表子项布局文件

  • custom_spinner_dropdown_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"xmlns:app="http://schemas.android.com/apk/res-auto"><TextViewandroid:id="@+id/text_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@color/text_black"android:textSize="@dimen/sp_20"android:paddingHorizontal="@dimen/dp_10"android
http://www.xdnf.cn/news/19922.html

相关文章:

  • 面试复习题-Flutter场景题
  • 数据结构:双向链表
  • 题解:UVA1589 象棋 Xiangqi
  • 基于 CC-Link IE FB 转 DeviceNet 技术的三菱 PLC 与发那科机器人在汽车涂装线的精准喷涂联动
  • Augmentcode免费额度AI开发WordPress商城实战
  • 【全面指南】Claude Code 从入门到精通:安装、配置、命令与高级技巧详解
  • 一个线程池的工作线程run函数的解析
  • Docker 学习笔记
  • 52DH Pro网址导航系统开源版
  • 泰酷辣!我的头像被「移乐AI头像」‘爆改’成顶流了!免费快来薅!
  • 【FastDDS】Layer DDS之Domain (01-overview)
  • 深度学习之第六课卷积神经网络 (CNN)如何保存和使用最优模型
  • 因果机器学习热度攀升,成顶会顶刊 “加分项”,想发论文就认准它!
  • 苍穹外卖项目实战(日记十四)-记录实战教程及问题的解决方法-(day3课后作业) 菜品停售启售功能
  • 机器视觉中为什么优先选择黑白相机?
  • 【Linux】为什么死循环卡不死 Linux?3 个核心逻辑看懂进程优先级与 CPU 调度密码
  • 性能测试-jmeter9-直连数据库
  • 苍穹外卖项目笔记day03
  • 从0 死磕全栈第3天:React Router (Vite + React + TS 版):构建小时站实战指南
  • 机器学习-逻辑回归
  • raspberry Pi 4B(树莓派4B)开启VNC服务 主机用VNC连接
  • 14、Docker构建后端镜像并运行
  • 关于SPI串口spidev接收数据不完整的问题
  • Moonchain:「新加坡大华银行」加持下连接现实金融与链上经济的价值通道
  • 大数据毕业设计选题推荐-基于大数据的电信客户流失数据分析系统-Hadoop-Spark-数据可视化-BigData
  • 03、Maven下载与阿里云镜像加速
  • 电子电气架构 --- 新EEA架构下开发模式转变
  • Openmanus复现教程:打造自己的Agent助手
  • Python之split - 常遇见的bug
  • Redis突然挂了,数据丢了多少?就看你用RDB还是AOF