实现简单的多选下拉"spinner"

本文介绍了如何使用PopupWindow替代系统Spinner,创建一个自定义的多选下拉选择器。通过设置样式、布局和适配器,实现了一个包含年份和月份的选择功能。同时,讨论了如何设计更通用的下拉布局,以适应不同数量的选项,通过动态添加View和ListView实现灵活扩展。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         在这里之所以是带引号的Spinner是因为他并不是使用的系统的spinner,而是利用popupwindow来实现的一个spinner.

      效果图如下:(比较粗糙,可以仿照原理美化下)

     

_________________________________________________________________________


     下面正式介绍

     首先第一步:上边的两个类似spinner的东西,

     事先准备好9.png图片

      

      写一个style作为我们spinner实现的样式

style.xml

<style name="spinner_style">
        <item name="android:background">@drawable/spinner</item>
        <item name="android:padding">4dp</item>
    </style>
    接着构造我们两个spinner的布局在其中引用我们的style.

layout_month_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#ffffff"
        android:gravity="center">
    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:src="@drawable/date"/>
    </LinearLayout>
    <TextView
        android:id="@+id/year"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:textColor="#000000"
        android:gravity="center"
        style="@style/spinner_style">

    </TextView>
    <TextView
        android:id="@+id/month"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:textColor="#000000"
        android:gravity="center"
        style="@style/spinner_style">
    </TextView>

</LinearLayout>
    

     开始定义我们的控件:

    

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by lenovo on 2016/11/2.
 */
public class MonthSelector extends LinearLayout{
    private View root;
    private TextView yearText;
    private TextView monthText;
    private MonthSelectorDorp drop;//后面定义
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值