如何扩展Android Material Dark主题

时间:2020-05-04 12:26:27

标签: android material-design

我知道我可能在这里犯了足够多的错误。但是莱姆知道吗。 我已经尝试过以最好的方式浏览MaterialIO文档,并且看起来很混乱,每页中都有许多链接,将我重定向到另一个次要组件。 那是我决定尝试的时候。

  • 我在我的依赖项中添加了materialIO内容
  • 然后我将Material添加到清单文件中,并写下了
<application ...
        android:theme="@style/Theme.MaterialComponents.DayNight" >
</application>
  • 接下来,我按照材料IO页面的某些部分分别制作了values/themes.xmlvalues-night/themes.xml
  • 我的直觉是在那些扩展了Material主题并设置我的颜色值的资源中编写样式标签。我从materialIO页面找到的颜色:
    • 背景(0dp高程表面覆盖)->
    • 表面(具有1dp高程表面覆盖)->
    • 主要
    • 中学
    • 背景
    • 在表面上
    • 在小学
    • 在中学
  • 因此,我开始将标签添加到values-night / themes.xml中。我不能将android:应用于某些标签,而某些标签却出现错误。
<?xml version="1.0" encoding="utf-8"?>
<resources>
        <style name="AppCompat.." parent="Theme.MaterialComponents.DayNight">
                <item name="android:colorBackground">@color/JDBG</item>
                <item name="colorSurface">@color/JDSurface</item>
                <item name="colorPrimary">@color/JDPrimary</item>
                <item name="android:colorSecondary">@color/JDSecondary</item>
                <item name="android:OnBackground">@color/JDOnBackground</item>
                <item name="android:OnSurface">@color/JDOnSurface</item>
                <item name="OnPrimary">@color/JDOnPrimary</item>
                <item name="OnSecondary">@color/JDOnSecondary</item>
        </style>
</resources>
  • 然后我将颜色分别添加到colors.xmlcolors-night.xml文件中,这告诉我这些值在基本值文件夹中没有声明,这可能会引起问题。

我不想花很多精力去编写非常抽象的文档,它会引导我在每个方向上走十个方向。因此是一个问题。 。 。 我做错了什么吗? 我下一步该怎么做?

1 个答案:

答案 0 :(得分:1)

只需在资源文件中定义主题。
例如res/values/styles.xml

<style name="MyTheme" parent="Theme.MaterialComponents.DayNight">
    <item name="colorPrimary">@color/....</item>
    <item name="colorSecondary">@color/....</item>
    <item name="colorOnPrimary">@color/....</item>
    <item name="colorOnSecondary">@color/....</item>
    .....
</style>

res/values/colors.xmlres/values-night/colors.xml中定义颜色。

然后在清单中应用主题:

<application ...
        android:theme="@style/MyTheme" >

还要检查official guide.

相关问题