You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Material TimePicker深色主题适配失效问题求助

Fix: Material TimePicker Dark Theme Not Applying When App is Manually Set to Dark (System Light)

Let’s break down why this is happening and how to fix it:

The core issue here is that when you manually set your app to dark theme (instead of following the system), the MaterialTimePicker isn’t picking up your custom dark theme overlay by default. When following the system, the DayNight parent theme automatically syncs the picker’s theme with the system’s dark/light state—but when overriding the app theme manually, we need to explicitly tell the picker which theme to use.

Step 1: Verify Your Theme Setup

First, double-check that your dark theme is properly configured to enforce the time picker theme. Your existing setup looks mostly right, but ensure your DarkThemeParent is correctly applied when the app is in dark mode:

<!-- Dark theme parent (applied when app is set to dark) -->
<style name="DarkThemeParent" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
    <!-- Other dark theme attributes -->
    <item name="materialTimePickerTheme">@style/DarkMaterialTimePickerTheme</item>
</style>

<!-- Time picker overlay for dark theme -->
<style name="DarkMaterialTimePickerTheme" parent="ThemeOverlay.MaterialComponents.TimePicker">
    <item name="colorPrimary">@color/primaryColor</item>
    <!-- Add other dark-specific styles here if needed, like text color -->
    <item name="colorOnSurface">@color/white</item>
</style>

Step 2: Explicitly Set the Theme in the TimePicker Builder

The key fix is to explicitly pass your dark time picker theme to the MaterialTimePicker.Builder. This ensures the picker uses your app’s dark theme regardless of the system’s theme state:

val picker: MaterialTimePicker = MaterialTimePicker.Builder()
    .setHour(morningCalendar.get(Calendar.HOUR_OF_DAY))
    .setMinute(morningCalendar.get(Calendar.MINUTE))
    // Explicitly apply your dark time picker theme here
    .setTheme(R.style.DarkMaterialTimePickerTheme)
    .build()

Alternative: Use Context with the Correct Theme

If you want the picker to automatically adapt to your app’s current theme (without hardcoding the dark theme), you can wrap your context with the app’s current theme before creating the builder:

// Get the current app theme (dark in this case)
val themedContext = ContextThemeWrapper(context, R.style.DarkThemeParent)
val picker: MaterialTimePicker = MaterialTimePicker.Builder(themedContext)
    .setHour(morningCalendar.get(Calendar.HOUR_OF_DAY))
    .setMinute(morningCalendar.get(Calendar.MINUTE))
    .build()

Why This Works

When you follow the system theme, the DayNight parent theme handles propagating the correct overlay to the picker. But when you manually set the app to dark, the picker defaults to using the system’s theme unless you explicitly tell it to use your app’s dark theme configuration. By adding .setTheme() or using a themed context, you force the picker to respect your app’s current theme state.

内容的提问来源于stack exchange,提问作者ilyas ipek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 05:47:45