简体   繁体   English

深色主题安卓应用

[英]Dark theme android app

I'm trying to create a dark theme similar to the one in OxygenOS on OnePlus devices.我正在尝试在 OnePlus 设备上创建一个类似于 OxygenOS 中的黑暗主题。

在此处输入图片说明

I changed the window background to black but the problem is the action bar is not becoming pure black.我将窗口背景更改为黑色,但问题是操作栏没有变成纯黑​​色。

<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>

SIMPLEST SOLUTION最简单的解决方案

You can enable/disable dark theme just by:您可以通过以下方式启用/禁用深色主题:

  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, ie 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根据黑暗模式的移动设置设置应用程序主题,即如果启用了黑暗模式,则主题将设置为黑暗主题,如果不是,则为默认主题,但这仅适用于版本 >= Android 版本 Q

     AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)

Notes:笔记:

  1. Your base theme for app/activity should be您的应用程序/活动的基本主题应该是

"Theme.AppCompat.DayNight" “Theme.AppCompat.DayNight”

like喜欢

<style name="DarkTheme" parent="Theme.AppCompat.DayNight">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
  1. Your res folder's names would end with -night so that different colors and images you can set for day and night themes like您的 res 文件夹的名称将以 -night 结尾,以便您可以为白天和黑夜主题设置不同的颜色和图像,例如

drawable & drawable-night,可绘制和可绘制之夜,
values & values-night价值观和价值观之夜

Replace these values in your colors.xml在您的colors.xml 中替换这些值

<color name="colorPrimary">#101010</color>
<color name="colorPrimaryDark">#000000</color>

This would be enough to change the Toolbar's color.这足以改变工具栏的颜色。


If you don't want to change the whole app primary color (which seems it is what you were trying to do in the first place), try creating a new Toolbar by:如果您不想更改整个应用程序的原色(这似乎是您首先要做的),请尝试通过以下方式创建一个新的工具栏:

Add this to your app's build.gradle将此添加到您的应用程序的 build.gradle

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

Add this to your main layout (activity_main.xml)将此添加到您的主布局 (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>

Set this in your styles (styles.xml):在您的样式 (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>

And set the new toolbar (MainActivity.java).并设置新的工具栏(MainActivity.java)。

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

Implementing a dark theme (or black theme) is very easy today thanks to由于今天实施深色主题(或黑色主题)非常容易

Theme.DayNight and AppCompatDelegate Theme.DayNight 和 AppCompatDelegate

Android comes to us allowing us to declare Android 来找我们允许我们声明

night/colors.xml晚上/colors.xml

and

night/styles.xml晚上/styles.xml

Full example here: https://github.com/android/user-interface-samples/tree/master/DarkTheme完整示例: https : //github.com/android/user-interface-samples/tree/master/DarkTheme

With the Material Components we can use two values folder ie values (for light theme) & values-night (for dark theme) or can also manage the dayNight theme with the styling using:使用 Material Components,我们可以使用两个值文件夹,即 values(对于浅色主题)和 values-night(对于深色主题),或者也可以使用以下样式管理dayNight主题:

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

For a Dark Action Bar a theme parent should be:对于深色操作栏,主题父项应该是:

 parent="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"

To set Dark theme we can use this method of AppCompatDelegate要设置深色主题,我们可以使用 AppCompatDelegate 的这种方法

setDefaultNightMode(
            AppCompatDelegate.MODE_NIGHT_YES);

More details on how this works with different API levels. 有关如何在不同 API 级别下工作的更多详细信息。

In order to support Dark Theme first thing you will need to do is to make sure your theme inherits from DayNight theme.为了支持黑暗主题,您需要做的第一件事是确保您的主题继承自 DayNight 主题。

After you're done, the theme should look like the following:完成后,主题应如下所示:

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

Essentially the DayNight theme works with two directories - Light theme inside values directory and Dark inside values-night directory.本质上,DayNight 主题适用于两个目录 - values 目录中的 Light 主题和 values-night 目录中的 Dark。

Therefore whenever you want to use different resource values, depending on the theme, you need to declare that resource in both above directories.因此,每当您想使用不同的资源值时,根据主题,您需要在上述两个目录中声明该资源。

For instance you could create two different color resources like this:例如,您可以像这样创建两种不同的颜色资源:

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>

That should be it, you can read about it in more detail on my blog应该是这样,你可以在我的博客上更详细地阅读它

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

No one mention about this as the other answer implying that Dark = Night.没有人提到这一点作为暗示黑暗 = 夜晚的另一个答案。 No!不! Dark Mode is not Night Mode . Dark Mode不是Night Mode They are completely different.他们是完全不同的。 DM was introduce in Android 10 that enforce built-in black and white color thus it can be change via device settings while NM exist already on earlier version that uses either default/defined style depending on your implementation and usually can be change inside the app settings. DM 是在 Android 10 中引入的,它强制执行内置的黑白颜色,因此它可以通过设备设置进行更改,而 NM 已经存在于早期版本中,根据您的实现使用默认/定义的样式,通常可以在应用程序设置中更改. If you want your app to use your define light/night style and not relying on built-in dark style, you may want to forceDarkAllowed to false in themes.xml or style.xml as DM may conflict.如果您希望您的应用程序使用您定义的光/夜风格而不依赖于内置的黑暗风格,您可能需要在 themes.xml 或 style.xml 中forceDarkAllowed为 false,因为 DM 可能会发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM