暗黑主题安卓应用程序

12

我正在尝试创建类似于一加设备上OxygenOS的暗黑主题。

在此输入图片描述

我将窗口背景改为黑色,但问题是操作栏没有变成纯黑色。

<style name="DarkTheme" parent="Theme.AppCompact">
    <item name="android:colorPrimary">@color/black</item>
    <item name="android:colorPrimaryDark">@color/black</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:colorAccent">@color/white</item>
    <item name="android:color">@color/white</item>
    <item name="android:windowBackground">@color/black</item>
    <item name="android:navigationBarColor">@color/black</item>
    <item name="android:actionBarStyle">@color/black</item>
</style>
6个回答

16

最简单的解决方案

您可以通过以下方式启用/禁用暗色主题:

  1. enable dark theme:

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
    
  2. forcefully disable dark theme:

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
    
  3. set app theme based on mobile settings of dark mode, i.e. if dark mode is enabled then the theme will be set to a dark theme, if not then the default theme, but this will only work in version >= Android version Q

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
    

注意:

  1. 您的应用程序/活动的基本主题应该是

"Theme.AppCompat.DayNight"

就像这样。

<style name="DarkTheme" parent="Theme.AppCompat.DayNight">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
  1. 您的res文件夹名称应以“-night”结尾,以便为白天和黑夜主题设置不同的颜色和图像,例如

drawable和drawable-night,
值和值之夜


8
请在您的colors.xml中替换这些值。
<color name="colorPrimary">#101010</color>
<color name="colorPrimaryDark">#000000</color>

这就足够改变工具栏的颜色了。
如果你不想改变整个应用程序的主色调(这似乎是你第一次尝试做的),那么可以通过以下方式创建一个新的工具栏:

将以下内容添加到应用程序的build.gradle文件中:

compile 'com.android.support:design:23.1.1'

将以下内容添加到主布局(activity_main.xml)中

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="mx.evin.apps.startingtemplate.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/a_main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/black"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

将以下内容设置在您的样式(styles.xml)中:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

设置新工具栏(MainActivity.java)。

Toolbar toolbar = (Toolbar) findViewById(R.id.a_main_toolbar);
setSupportActionBar(toolbar);

4

2

使用 Material Components 我们可以使用两个值文件夹,即 values(用于浅色主题)和 values-night(用于深色主题),也可以使用以下方式管理 dayNight 主题:

<style name="AppTheme" parent="Theme.AppCompat.DayNight">

为了实现深色操作栏,主题的父级应该是:
 parent="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"

我们可以使用AppCompatDelegate的这个方法来设置暗色主题

setDefaultNightMode(
            AppCompatDelegate.MODE_NIGHT_YES);

更多关于这个如何在不同的API级别下工作的细节。


1

没有人像其他答案暗示的那样提到这一点,即黑暗=夜晚。不!暗黑模式不等于夜间模式。它们是完全不同的。DM是在Android 10中引入的,强制使用内置的黑白色彩,因此可以通过设备设置进行更改,而NM已经存在于早期版本中,根据您的实现使用默认/定义的样式,并且通常可以在应用程序设置中更改。如果您希望您的应用程序使用您定义的浅色/夜间样式,而不是依赖内置的暗黑样式,则可能需要在themes.xml或style.xml中将forceDarkAllowed设置为false,因为DM可能会发生冲突。


1

为了支持暗黑主题,首先你需要确保你的主题继承自DayNight主题。

完成后,主题应该看起来像下面这样:

<style name="AppTheme" parent="Theme.AppCompat.DayNight">

基本上,DayNight主题使用两个目录 - values目录内的Light主题和values-night目录内的Dark主题。
因此,每当您想要使用不同的资源值,取决于主题,您需要在上述两个目录中声明该资源。
例如,您可以像这样创建两个不同的颜色资源:

values/colors.xml

<color name="colorPrimary">#f2f2f2</color>
<color name="colorPrimaryDark">#000000</color>
<color name="colorAccent">#29b6f6</color>

values-night/colors.xml

<color name="colorPrimary">#2e2f32</color>
<color name="colorPrimaryDark">#121212</color>
<color name="colorAccent">#90caf9</color>

那就这样了,你可以在我的博客上详细阅读。

https://androidexplained.github.io/ui/android/material-design/2020/09/24/dark-mode.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接