如何使用MaterialComponents正确显示DatePickerDialog? [英] How to properly display DatePickerDialog using MaterialComponents?

查看:89
本文介绍了如何使用MaterialComponents正确显示DatePickerDialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用来自material.io的Android的MaterialComponents.

I'm using MaterialComponents for Android from material.io.

我有两个主题(浅色和深色).我想显示一个DatePickerDialog. 但这是结果:

I have two themes (light and dark). I want to show an DatePickerDialog. But this is the result:

以下是我如何显示对话框的代码:

Here is the code how i show my dialog:

private fun selectDeadline() {
    val dialog = DateDialog()
    val bundle = Bundle()
    bundle.putInt("themeId", themeId)
    dialog.arguments = bundle
    dialog.show(supportFragmentManager, "")
}

class DateDialog : DialogFragment(), DatePickerDialog.OnDateSetListener {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val date = DateTime.now()
        val themeId = arguments!!.getInt("themeId")
        return DatePickerDialog(activity!!, themeId, this, date.year, date.monthOfYear - 1, date.dayOfMonth)
    }

    override fun onDateSet(view: DatePicker?, year: Int, month: Int, day: Int) {
        deadlineTitle.text = "$year, $month, $day"
    }
}

最后但并非最不重要的是,我的主题:

And last but not least, my themes:

<resources>

<!-- Dark application theme. -->
<style name="AppTheme.Dark" parent="Theme.MaterialComponents.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="toolbarBackground">@color/darkBackground</item>
    <item name="toolbarStyle">@style/AppTheme.Dark.Toolbar</item>
    <item name="iconColor">#fff</item>
    <item name="dividerBackground">@android:drawable/divider_horizontal_dark</item>

    <item name="android:statusBarColor">@android:color/black</item>
</style>

<style name="AppTheme.Dark.Toolbar" parent="Widget.AppCompat.Toolbar">
    <item name="android:background">@color/darkBackground</item>
    <item name="android:elevation">8dp</item>
</style>



<!-- Light application theme. -->
<style name="AppTheme.Light" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="toolbarBackground">@color/lightBackground</item>
    <item name="toolbarStyle">@style/AppTheme.Light.Toolbar</item>
    <item name="iconColor">#000</item>
    <item name="dividerBackground">@android:drawable/divider_horizontal_bright</item>

    <item name="android:datePickerDialogTheme">@style/AppTheme.Light.DatePicker</item>

    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>

<style name="AppTheme.Light.DatePicker" parent="ThemeOverlay.MaterialComponents.Dialog">
    <item name="materialButtonStyle">?android:attr/borderlessButtonStyle</item>
</style>

<style name="AppTheme.Light.Toolbar" parent="Widget.MaterialComponents.Toolbar">
    <item name="android:background">@color/colorPrimary</item>
    <item name="android:elevation">8dp</item>
</style>

如果我不给themeId作为参数,则它在浅色主题上正确显示,而在深色主题上,它仅显示带有白色文本的浅色版本,由于白色背景,用户看不见.

If i don't give themeId as parameter it shows properly on light theme but on dark theme it shows just the light version with white text which is invisible for the user to see because of the white background.

解决此问题的正确方法是什么?

What is the proper way to solve this problem?

推荐答案

感谢您提出问题.材料组件库当前不支持日期和时间选择器的暗模式.我们计划为此添加支持,请继续关注.

Thank you for the question. The Material Components library currently doesn't support dark mode for date and time pickers. We plan to add support for this, please stay tuned.

这篇关于如何使用MaterialComponents正确显示DatePickerDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆