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

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

    

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

解决办法

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

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

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

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

pop_date_picker.xml





    

        

            

            
        

        

        


            


            
        
    



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 = 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 代码中动态设置颜色 主题)