美文网首页
DatePickerDialog 代码中动态设置颜色 主题

DatePickerDialog 代码中动态设置颜色 主题

作者: 夜明智灵 | 来源:发表于2019-05-05 18:03 被阅读0次

    原生自带的DatePickerDialog 只能通过 style设置主题色
    在style中添加样式修改android:colorAccent的颜色。然后在DatePickerDialog的构造方法在传入这个样式。

        <style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog">
            <item name="android:colorAccent">@color/bg_color</item>
        </style>
    

    但是 这样不能在代码中动态的改变他的主题颜色值。很多情况需要去服务器拉取主题色的配置项。主题色是动态生的

    解决办法

    使用三方的DatePicker 然后封装一个和原生一模一样

    Screenshot_2019-05-05-17-57-10-529_com.chinamclou.png

    这里用到了github的一个开源库。
    https://github.com/prolificinteractive/material-calendarview

    这个和原版稍微有些不同。这里你需要下载源码,把其中的一些格式化规则修改一下就和原生一样了。然后对其封装成PopupWindow

    pop_date_picker.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout
    
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
        <LinearLayout
                android:orientation="vertical"
                android:background="@android:color/white"
                android:layout_width="260dp"
                android:layout_height="wrap_content">
    
            <LinearLayout
                    android:id="@+id/titleLayout"
                    android:padding="14dp"
                    android:background="@android:color/holo_red_light"
                    android:layout_width="match_parent"
                    android:orientation="vertical"
                    android:layout_height="wrap_content">
    
                <TextView
                        android:text="2019年"
                        android:textSize="14sp"
                        android:id="@+id/titleYear"
                        android:textColor="@android:color/white"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>
    
                <TextView
                        android:id="@+id/titleDate"
                        android:text="5月28日周二"
                        android:textSize="24sp"
                        android:textColor="@android:color/white"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>
            </LinearLayout>
    
            <com.prolificinteractive.materialcalendarview.MaterialCalendarView
                    android:id="@+id/calendarView"
                    android:paddingStart="4dp"
                    android:paddingEnd="4dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:mcv_showOtherDates="none"
                    app:mcv_weekDayLabels="@array/week_label"
                    app:mcv_monthLabels="@array/monthLabels"
                    app:mcv_selectionColor="#00F"/>
    
            <RelativeLayout
    
                    android:layout_width="wrap_content"
                    android:layout_height="60dp">
    
    
                <TextView
                        android:id="@+id/sure"
                        android:layout_alignParentEnd="true"
                        android:textSize="12sp"
                        android:layout_margin="14dp"
                        android:layout_alignParentBottom="true"
                        android:layout_width="wrap_content"
                        android:text="确定"
                        android:layout_height="wrap_content"/>
    
    
                <TextView
                        android:id="@+id/cancel"
                        android:layout_toStartOf="@+id/sure"
                        android:textSize="12sp"
                        android:layout_margin="14dp"
                        android:layout_alignParentBottom="true"
                        android:layout_width="wrap_content"
                        android:text="取消"
                        android:layout_height="wrap_content"/>
            </RelativeLayout>
        </LinearLayout>
    
    
    </RelativeLayout>
    

    DatePickerPop 这里使用的是kotlin 可以自己转成java或配置kotlin环境

    class DatePickerPop(
        context: Context
    ) :
        BasePopWin(context) {
        lateinit var mCalendarDay: CalendarDay
        var onDateSelectedListener: OnDateSelectedListener? = null
        lateinit var titleLayout: LinearLayout
        lateinit var calendarView: MaterialCalendarView
        lateinit var titleYear: TextView
        lateinit var titleDate: TextView
        lateinit var sure: TextView
        lateinit var cancel: TextView
        var weekArray: Array<String> = arrayOf("周一", "周二", "周三", "周四", "周五", "周六", "周日")
        override fun layout(): Int {
            return R.layout.pop_date_picker
        }
    
        override fun onView(mRootView: View) {
            sure = mRootView.findViewById(R.id.sure)
            cancel = mRootView.findViewById(R.id.cancel)
            titleLayout = mRootView.findViewById(R.id.titleLayout)
            calendarView = mRootView.findViewById(R.id.calendarView)
            titleYear = mRootView.findViewById(R.id.titleYear)
            titleDate = mRootView.findViewById(R.id.titleDate)
            calendarView.state()
                .edit()
                .setMinimumDate(CalendarDay.from(2010, 1, 1))
                .setMaximumDate(CalendarDay.from(2030, 12, 31))
                .commit()
    
            calendarView.setOnDateChangedListener { _, date, _ ->
                titleYear.text = "${date.year}年"
                titleDate.text = "${date.month}月${date.day}日${weekArray[date.week - 1]}"
                mCalendarDay =date
            }
            cancel.setOnClickListener { dismiss() }
            sure.setOnClickListener {
                onDateSelectedListener?.onDate(mCalendarDay.year, mCalendarDay.month, mCalendarDay.day)
                dismiss()
            }
        }
    
    
        fun showDatePicker(
            view: View?,
            color: Int,
            year: Int,
            month: Int,
            day: Int
        ) {
            mCalendarDay = CalendarDay.from(year, month, day)
            calendarView.currentDate = mCalendarDay
            titleYear.text = "$year 年"
            titleDate.text = "$month 月$day 日${weekArray[mCalendarDay.week - 1]}"
            calendarView.selectedDate = mCalendarDay
            calendarView.selectionColor = color
            titleLayout.setBackgroundColor(color)
            cancel.setTextColor(color)
            sure.setTextColor(color)
            showAtLocation(view, Gravity.CENTER, 0, 0)
        }
    
        public interface OnDateSelectedListener {
            fun onDate(year: Int, month: Int, day: Int)
        }
    

    再贴一个BasePopWin

    public abstract class BasePopWin extends PopupWindow implements PopupWindow.OnDismissListener {
        private Context context;
    
        public BasePopWin(Context context) {
            this(context, null);
        }
    
        public BasePopWin(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.context = context;
    
            init();
        }
    
        private void init() {
            View mRootView = LayoutInflater.from(context).inflate(layout(), null, false);
            mRootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            setBackgroundDrawable(new ColorDrawable());
            setContentView(mRootView);
            onView(mRootView);
            setTouchable(true);
            setFocusable(true);
            setOutsideTouchable(false);
            setOnDismissListener(this);
        }
    
        public abstract int layout();
    
        public abstract void onView(View mRootView);
    
        @Override
        public void onDismiss() {
            alpha(1f);
        }
    
        @Override
        public void showAsDropDown(View anchor) {
            super.showAsDropDown(anchor);
        }
    
        @Override
        public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
            alpha(0.4f);
            super.showAsDropDown(anchor, xoff, yoff, gravity);
        }
    
        @Override
        public void showAtLocation(View parent, int gravity, int x, int y) {
            alpha(0.4f);
            super.showAtLocation(parent, gravity, x, y);
        }
    
        private void alpha(float alpha) {
            if (context instanceof Activity) {
                Activity activity = (Activity) context;
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                lp.alpha = alpha;
                activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(lp);
            }
        }
    }
    

    showDatePicker传入定位到的年月日和主题颜色就可以了

    fun showDatePicker(
            view: View?,
            color: Int,
            year: Int,
            month: Int,
            day: Int
        )
    

    相关文章

      网友评论

          本文标题:DatePickerDialog 代码中动态设置颜色 主题

          本文链接:https://www.haomeiwen.com/subject/hpihoqtx.html