How to set custom colors for components (toolbar, seekbar,actionbar, etc.) when switching day/night mode?

huangapple go评论72阅读模式
英文:

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):

&lt;resources&gt;

    &lt;!-- Base application theme. --&gt;
    &lt;style name=&quot;MyTheme&quot; parent=&quot;Theme.MaterialComponents.DayNight.NoActionBar&quot;&gt;
        &lt;!-- Customize your theme here. --&gt;
        &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorPrimary&lt;/item&gt;
...
&lt;/resources&gt;

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" 属性,为每个工具栏设置不同的 shapecolor 和其他材料效果。

关于颜色:
通常情况下,你可以在你的 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>

重要链接:

  1. 官方材料设计指南 - 设计暗黑主题
  2. 官方材料设计指南 - 开发暗黑主题
  3. 使用 SwitchPreference 切换主题
  4. 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 inside values/styles.xml should extend from Theme.MaterialComponents.Light.DarkActionBar

  • Your Night theme inside values-night/styles.xml should extend from Theme.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):

&lt;resources&gt;
            &lt;!-- Base application theme. --&gt;
            &lt;style name=&quot;MyTheme&quot; parent=&quot;Theme.MaterialComponents.Light.NoActionBar&quot;&gt;
                &lt;!-- Customize your theme here. --&gt;
                &lt;item name=&quot;seekBarStyle&quot;&gt;@style/MySeekBarStyle&lt;/item&gt;
                &lt;item name=&quot;toolbarStyle&quot;&gt;@style/MyToolBarStyle&lt;/item&gt;
                &lt;item name=&quot;actionBarStyle&quot;&gt;@style/MyActionBarStyle&lt;/item&gt;
              &lt;item name=&quot;floatingActionButtonStyle&quot;&gt;@MyFloatingActionButtonStyle&lt;/item&gt;
                &lt;!--   a sample toolbar styling, choose parent with care --&gt;
                &lt;!--    your AppTheme inheriting from MaterialComponents but toolbar 
                &lt;!--   inheriting from platform theme--&gt;
                &lt;!--    may case weird visual effects--&gt;
    &lt;style name=&quot;MyToolBarStyle&quot; parent=&quot;Widget.MaterialComponents.Toolbar&quot;&gt;
            &lt;item name=&quot;titleTextColor&quot;&gt;@color/lightWhite&lt;/item&gt;
            &lt;item name=&quot;subtitleTextColor&quot;&gt;@color/lightWhite&lt;/item&gt;
        &lt;/style&gt;
        &lt;/resources&gt;

If you want to style each for eg. ToolBar differently, you can use style=&quot;@style/MyToolBarStyle&quot; 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.

 &lt;!-- primary colour of your brand and its variants --&gt;
        &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorPrimary700&lt;/item&gt;
        &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/colorPrimary900&lt;/item&gt;
        &lt;item name=&quot;colorPrimaryVariant&quot;&gt;@color/colorPrimary500&lt;/item&gt;

        &lt;!-- colour which contrasts from your primary colour --&gt;
        &lt;item name=&quot;colorAccent&quot;&gt;@color/colorAccent&lt;/item&gt;

        &lt;!--secondary colour of your brand and its variants --&gt;
        &lt;item name=&quot;colorSecondary&quot;&gt;@color/colorSecondary700&lt;/item&gt;
        &lt;item name=&quot;colorSecondaryVariant&quot;&gt;@color/colorSecondary500&lt;/item&gt;

        &lt;!--background color for your root layout file --&gt;
        &lt;item name=&quot;android:colorBackground&quot;&gt;@android:color/white&lt;/item&gt;

        &lt;!--background color of children view --&gt;
        &lt;item name=&quot;colorSurface&quot;&gt;@color/lightWhite&lt;/item&gt;

        &lt;!--color to show error mostly it will be red or orange
        &lt;item name=&quot;colorError&quot;&gt;@color/colorErrorAlternate&lt;/item&gt;

        &lt;!-- These are colors which constrasts colors defined for --&gt;
        &lt;!-- primary, secondary, bg, surface, error respectively. --&gt;
        &lt;!-- For eg: TextViews in Toolbar colored with colorPrimary --&gt;
        &lt;!-- will use colorOnPrimary as their text color --&gt;

        &lt;item name=&quot;colorOnPrimary&quot;&gt;@color/lightWhite&lt;/item&gt;
        &lt;item name=&quot;colorOnSecondary&quot;&gt;@color/lightDark&lt;/item&gt;
        &lt;item name=&quot;colorOnBackground&quot;&gt;@color/lightDark&lt;/item&gt;
        &lt;item name=&quot;colorOnSurface&quot;&gt;@color/lightDark&lt;/item&gt;
        &lt;item name=&quot;colorOnError&quot;&gt;@color/lightDark&lt;/item&gt;

Important Links:

  1. Official Material Design guide for designing dark theme
  2. Official Material Design guide for developing dark theme
  3. Using switchpreference to switch theme
  4. Nick Butcher's Medium Blog : you will know more about colours here

huangapple
  • 本文由 发表于 2020年5月30日 07:44:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/62096103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定