支持黑暗与黑暗;AndroidX中的光主题

支持黑暗与黑暗;AndroidX中的光主题,android,android-dark-theme,Android,Android Dark Theme,我目前使用以下父主题theme.MaterialComponents.Light.NoActionBar,并将我的材质设计库更新为 implementation'com.google.android.material:material:1.1.0' 这把我的应用程序中的一些颜色搞砸了 所以我决定更新它,以支持光明和黑暗的主题。我将发布我为实现这一目标所做的工作,以节省其他人搜索的时间在执行一些搜索后,这是我在详细信息中所做的工作 将父主题更改为主题.MaterialComponents.DayN

我目前使用以下父主题
theme.MaterialComponents.Light.NoActionBar
,并将我的材质设计库更新为

implementation'com.google.android.material:material:1.1.0'

这把我的应用程序中的一些颜色搞砸了


所以我决定更新它,以支持光明和黑暗的主题。我将发布我为实现这一目标所做的工作,以节省其他人搜索的时间

在执行一些搜索后,这是我在详细信息中所做的工作

将父主题更改为
主题.MaterialComponents.DayNight.NoActionBar

    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
      <item name="android:navigationBarColor">@color/transparent</item>
      <item name="android:windowContentTransitions" tools:targetApi="lollipop">true</item>
      <item name="colorPrimary">@color/colorPrimary</item>
      <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
      <item name="colorSecondary">@color/colorAccent</item>
    </style>
示例

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="colorPrimaryDark">#1f2021</color>

</resources>
setTextColor(Utils.getColor(context,R.attr.colorError))


  • 如有必要,在夜间目录中添加值,以支持黑暗和夜间模式下的不同颜色
  • 在目录中添加colors.xml文件,然后只覆盖colors.xml中写入的任何颜色
示例

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="colorPrimaryDark">#1f2021</color>

</resources>

#1f2021

黑色主题:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
对于普通主题:

 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
您可以在oncreate中根据需要设置主题,并单击按钮等

style.xml
文件夹

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@android:color/black </item>
    </style>

</resources>
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/orange</item>
        <item name="colorPrimaryDark">@color/orangeDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@android:color/white</item>
    </style>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="daynight_textColor">#6cbabb</color>

</resources>
<resources>
 <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="daynight_textColor">#ff8222</color>
</resources>
值的
color.xml
文件夹

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@android:color/black </item>
    </style>

</resources>
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/orange</item>
        <item name="colorPrimaryDark">@color/orangeDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@android:color/white</item>
    </style>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="daynight_textColor">#6cbabb</color>

</resources>
<resources>
 <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="daynight_textColor">#ff8222</color>
</resources>