英文:
How to set custom colors for components (toolbar, seekbar,actionbar, etc.) when switching day/night mode?
问题
我正在尝试通过为日间模式和夜间模式设置自定义主题来更改应用程序组件的某些颜色,就像这样:
在 values/styles.xml(日间模式)中:
<resources>
<!-- Base application theme. -->
<style name="MyTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- 在这里自定义您的主题。 -->
<item name="colorPrimary">@color/colorPrimary</item>
...
</resources>
在 values/styles.xml(夜间模式)中:
我想要更改界面的某些部分的颜色(例如拖动条、工具栏、操作栏标题、浮动按钮等),但我不知道每个元素对应的颜色是什么,我正在疯狂地在各处搜索棘手的解决方案,以更改我需要改变颜色的任何组件。是否有任何指南或良好的视觉示例可以查看所有这些内容?例如,我现在花了很长时间来弄清楚如何更改弹出菜单的背景或操作栏菜单的背景,因为在菜单文件中没有相应的属性。我刚开始接触 Android 开发,所以对于这方面的任何指导都将不胜感激。
英文:
I'm trying to change some colors of the components of my application by setting a custom theme for day mode and night mode, like this:
in values/styles.xml (day mode):
<resources>
<!-- Base application theme. -->
<style name="MyTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
...
</resources>
Same file in values/styles.xml (night)
I want to change the color of some parts of the UI (seekbar bar, toolbar, action bar title, floating buttons etc.) but I don't know which color corresponds to each element and I'm kinda going crazy googling here and there for tricky solutions for any component I need to change the color too. Is there any guideline or good visual example on where to check all of this? For instance, right now it's taking a long time for me to figure out how to change the popupmenu background or the actionbar menu background since there's no attributes in the menu files. I'm new to android development so any kind of guidance regarding this would be very welcomed.
答案1
得分: 1
你使用了Theme.MaterialComponents.DayNight....
,其中DayNight
更多地是一种自适应的动态主题,会根据材料设计的默认颜色进行变化。如果你需要更多的颜色和样式控制,请按照以下方式操作:
-
你在
values/styles.xml
中的Day
主题应该继承自Theme.MaterialComponents.Light.DarkActionBar
-
你在
values-night/styles.xml
中的Night
主题应该继承自Theme.MaterialComponents
,因为它非常适合暗模式。
> 我想要改变界面某些部分的颜色(例如 seekbar、工具栏、操作栏标题、浮动按钮等)
关于这一点,如果你想要应用范围的变化,那么你可以按照以下的方式进行样式设置(几乎所有视图的样式都可以通过这种方式完成):
<resources>
<!-- 应用的基础主题 -->
<style name="MyTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- 在这里自定义你的主题 -->
<item name="seekBarStyle">@style/MySeekBarStyle</item>
<item name="toolbarStyle">@style/MyToolBarStyle</item>
<item name="actionBarStyle">@style/MyActionBarStyle</item>
<item name="floatingActionButtonStyle">@MyFloatingActionButtonStyle</item>
</style>
<!-- 一个示例工具栏样式,谨慎选择父主题 -->
<!-- 如果你的 AppTheme 继承自 MaterialComponents,但工具栏继承自平台主题 -->
<!-- 可能会导致奇怪的视觉效果 -->
<style name="MyToolBarStyle" parent="Widget.MaterialComponents.Toolbar">
<item name="titleTextColor">@color/lightWhite</item>
<item name="subtitleTextColor">@color/lightWhite</item>
</style>
</resources>
如果你想要为每个例如 ToolBar
设置不同的样式,你可以在你的布局文件中使用 style="@style/MyToolBarStyle"
属性,为每个工具栏设置不同的 shape
、color
和其他材料效果。
关于颜色:
通常情况下,你可以在你的 styles.xml
中调整这些颜色属性,以改变应用的整体外观和感觉。
<!-- 品牌主色及其变体 -->
<item name="colorPrimary">@color/colorPrimary700</item>
<item name="colorPrimaryDark">@color/colorPrimary900</item>
<item name="colorPrimaryVariant">@color/colorPrimary500</item>
<!-- 与主色对比的颜色 -->
<item name="colorAccent">@color/colorAccent</item>
<!-- 品牌次要色及其变体 -->
<item name="colorSecondary">@color/colorSecondary700</item>
<item name="colorSecondaryVariant">@color/colorSecondary500</item>
<!-- 根布局文件的背景颜色 -->
<item name="android:colorBackground">@android:color/white</item>
<!-- 子视图的背景颜色 -->
<item name="colorSurface">@color/lightWhite</item>
<!-- 错误显示的颜色,通常为红色或橙色 -->
<item name="colorError">@color/colorErrorAlternate</item>
<!-- 这些颜色对应于主色、次要色、背景色、表面色、错误色的对比颜色 -->
<!-- 例如:工具栏中的 TextView 使用 colorPrimary 作为文本颜色 -->
<!-- 将使用 colorOnPrimary 作为它们的文本颜色 -->
<item name="colorOnPrimary">@color/lightWhite</item>
<item name="colorOnSecondary">@color/lightDark</item>
<item name="colorOnBackground">@color/lightDark</item>
<item name="colorOnSurface">@color/lightDark</item>
<item name="colorOnError">@color/lightDark</item>
重要链接:
- 官方材料设计指南 - 设计暗黑主题
- 官方材料设计指南 - 开发暗黑主题
- 使用 SwitchPreference 切换主题
- Nick Butcher 的 Medium 博客 - 在这里你可以了解更多关于颜色的信息
英文:
You used Theme.MaterialComponents.DayNight....
where DayNight
is more of a adaptive dynamic theme which changes to material design default color. If you need more control to color and styles, do as follows:
-
Your
Day
theme insidevalues/styles.xml
should extend fromTheme.MaterialComponents.Light.DarkActionBar
-
Your
Night
theme insidevalues-night/styles.xml
should extend fromTheme.MaterialComponents
as it is well-suited for the Dark Mode.
> I want to change the color of some parts of the UI (seekbar bar, toolbar, action bar title, floating buttons etc.)
Regarding this, if you want app-wide changes then you can follow this method of styling(almost all views styling can be done this way):
<resources>
<!-- Base application theme. -->
<style name="MyTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="seekBarStyle">@style/MySeekBarStyle</item>
<item name="toolbarStyle">@style/MyToolBarStyle</item>
<item name="actionBarStyle">@style/MyActionBarStyle</item>
<item name="floatingActionButtonStyle">@MyFloatingActionButtonStyle</item>
<!-- a sample toolbar styling, choose parent with care -->
<!-- your AppTheme inheriting from MaterialComponents but toolbar
<!-- inheriting from platform theme-->
<!-- may case weird visual effects-->
<style name="MyToolBarStyle" parent="Widget.MaterialComponents.Toolbar">
<item name="titleTextColor">@color/lightWhite</item>
<item name="subtitleTextColor">@color/lightWhite</item>
</style>
</resources>
If you want to style each for eg. ToolBar
differently, you can use style="@style/MyToolBarStyle"
attributes in your layout files to give each of them different shape
, colour
and other material effects as you want.
About Colors:
Normally, you can play with these colour attributes in your styles.xml
to change the complete look and feel of your app.
<!-- primary colour of your brand and its variants -->
<item name="colorPrimary">@color/colorPrimary700</item>
<item name="colorPrimaryDark">@color/colorPrimary900</item>
<item name="colorPrimaryVariant">@color/colorPrimary500</item>
<!-- colour which contrasts from your primary colour -->
<item name="colorAccent">@color/colorAccent</item>
<!--secondary colour of your brand and its variants -->
<item name="colorSecondary">@color/colorSecondary700</item>
<item name="colorSecondaryVariant">@color/colorSecondary500</item>
<!--background color for your root layout file -->
<item name="android:colorBackground">@android:color/white</item>
<!--background color of children view -->
<item name="colorSurface">@color/lightWhite</item>
<!--color to show error mostly it will be red or orange
<item name="colorError">@color/colorErrorAlternate</item>
<!-- These are colors which constrasts colors defined for -->
<!-- primary, secondary, bg, surface, error respectively. -->
<!-- For eg: TextViews in Toolbar colored with colorPrimary -->
<!-- will use colorOnPrimary as their text color -->
<item name="colorOnPrimary">@color/lightWhite</item>
<item name="colorOnSecondary">@color/lightDark</item>
<item name="colorOnBackground">@color/lightDark</item>
<item name="colorOnSurface">@color/lightDark</item>
<item name="colorOnError">@color/lightDark</item>
Important Links:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论