如何解决将 applyDayNight() 与自定义主题叠加相结合?
在 androidx
中,您可以轻松地在日/夜模式之间切换。例如:
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- attributes -->
</style>
切换主题时:
AppCompatDelegate.setDefaultNightMode(nightMode);
getDelegate().applyDayNight();
<style name="LimeTheme" parent="AppTheme">
<item name="colorPrimary">@color/lime1</item>
<item name="colorPrimaryDark">@color/lime2</item>
<item name="colorControlHighlight">@color/lime3</item>
</style>
我如何做到这一点?
解决方法
可能你需要一个文件夹——[values-night]。
在你的theme.xml(或style.xml)中,你可以像这样设置日主题:
<style name="Theme.MyTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar" >
<item name="minor customization">@style/ThemeOverlay.MyTheme.DayCustom</item>
</style>
在你的 theme-night.xml(或 style-night.xml)中:
<style name="Theme.MyTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar" >
<item name="minor customization">@style/ThemeOverlay.MyTheme.NightCustom</item>
</style>
在风格上,你应该初始化这些:
<style name="ThemeOverlay.MyTheme.NightCustom" parent="">
<item name="colorPrimary">@color/nightLime1</item>
<item name="colorPrimaryDark">@color/nightLime2</item>
<item name="colorControlHighlight">@color/nightLime3</item>
</style>
<style name="ThemeOverlay.MyTheme.DayCustom" parent="">
<item name="colorPrimary">@color/dayLime1</item>
<item name="colorPrimaryDark">@color/dayLime2</item>
<item name="colorControlHighlight">@color/dayLime3</item>
</style>
key在ThemeOverlay中,ThemeOverlay.MyTheme.DayCustom或ThemeOverlay.MyTheme.NightCustom的父级为"",因为系统会自动识别它是ThemeOverlay,只需改变样式你设置,比如 colorPrimary,colorPrimaryDark...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。