黑暗主题Android应用程序

时间:2016-01-22 23:36:26

标签: android themes android-theme

我试图在OnePlus设备上创建类似于OxygenOS中的黑暗主题。

enter image description here

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

<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 个答案:

答案 0 :(得分:9)

最简单的解决方案

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

  1. 启用深色主题:

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
    
  2. 强制禁用深色主题:

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
    
  3. 根据暗模式的移动设置来设置应用主题,即,如果启用了暗模式,则该主题将设置为暗主题;如果未启用,则 默认主题,但这仅适用于版本> = Android版本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结尾,以便您可以为日夜主题设置不同的颜色和图像,例如

可绘制和可绘制的夜晚,
价值观与价值观之夜

答案 1 :(得分:5)

替换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);

答案 2 :(得分:3)

由于

,今天实现深色主题(或黑色主题)非常容易。

Theme.DayNight和 AppCompatDelegate

Android来了,允许我们声明

night / colors.xml

night / styles.xml

此处的完整示例:https://github.com/android/user-interface-samples/tree/master/DarkTheme

答案 3 :(得分:2)

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

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

对于Dark Action Bar,主题父项应为:

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

要设置深色主题,我们可以使用AppCompatDelegate的此方法

setDefaultNightMode(
            AppCompatDelegate.MODE_NIGHT_YES);

More详细介绍了如何在不同的API级别上使用。

答案 4 :(得分:0)

要支持黑暗主题,您需要做的第一件事就是确保您的主题继承自DayNight主题。

完成后,主题应如下所示:

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

本质上,DayNight主题可用于两个目录-值目录内的浅色主题和值-夜间目录内的深色主题。

因此,每当要根据主题使用不同的资源值时,都需要在上述两个目录中声明该资源。

例如,您可以创建两个不同的颜色资源,如下所示:

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

答案 5 :(得分:0)

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

相关问题