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
values-night/colors.xml