Android 暗黑模式适配

2022年12月27日10:25:51

Android 暗黑模式适配

不爱废话直接上菜。

第一种适配方式:

1.1 直接修改APP主题,values/styles.xml 如下:

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="android:forceDarkAllowed">true</item>
    ...
</style>
或者
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <item name="android:forceDarkAllowed">true</item>
    ...
</style>

1.2 修改AndroidManifest.xml中application标签下theme属性

<application
    android:name=".BaseApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    ....
</application>

1.3 说明:通过强制设置黑暗模式,可以使APP进行黑白两色进行反色(个人不推荐这么干)。

第二种适配方式:

2.1 修改APP主题,values/styles.xml 如下(注意这里forceDarkAllowed为false):

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="android:forceDarkAllowed">false</item>
    ...
</style>
或者
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <item name="android:forceDarkAllowed">false</item>
    ...
</style>

2.2 修改AndroidManifest.xml中application标签下theme属性

<application
	android:name=".BaseApplication"
	android:allowBackup="true"
	android:icon="@mipmap/ic_launcher"
	android:label="@string/app_name"
	android:roundIcon="@mipmap/ic_launcher_round"
	android:supportsRtl="true"
	android:theme="@style/AppTheme">
	....
</application>

2.3 颜色适配:在res目录下新建values-night目录,看名字就知道这个目录下的东西会在暗黑模式下生效(一般放colors.xml, styles.xml就可以了)(具体颜色是什么就可以找你们的设计师小伙伴了)。

values/colors.xml
Android 暗黑模式适配

values-night/colors.xml
Android 暗黑模式适配

2.4 图片适配:在res目录下新建mipmap-night-xhdpi / mipmap-night-xxhdpi目录,这些目录下的图片也是在暗黑模式下才生效(图片找设计师小伙伴)。

2.5 比较推荐使用第二种,可以达到与设计师要求的效果一致。

另外给大家一个深色模式适配推荐颜色图

Android 暗黑模式适配

  • 作者:环山绕水
  • 原文链接:https://blog.csdn.net/weixin_42169702/article/details/110423866
    更新时间:2022年12月27日10:25:51 ,共 1577 字。