Dark theme android app

Mar*_*Ben 2 android themes android-theme

I'm trying to create a dark theme similar to the one in OxygenOS on OnePlus devices.

在此处输入图片说明

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>
Run Code Online (Sandbox Code Playgroud)

Kis*_*nki 13

最简单的解决方案

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

  1. 启用深色主题:

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 强行禁用深色主题:

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
    
    Run Code Online (Sandbox Code Playgroud)
  3. 根据黑暗模式的移动设置设置应用程序主题,即如果启用了黑暗模式,则主题将设置为黑暗主题,如果不是则为默认主题,但这仅适用于版本 >= Android 版本 Q

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
    
    Run Code Online (Sandbox Code Playgroud)

笔记:

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

“Theme.AppCompat.DayNight”

喜欢

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

drawable & drawable-night,
values & values-night


Evi*_*n1_ 7

将这些值替换为colors.xml

<color name="colorPrimary">#101010</color>
<color name="colorPrimaryDark">#000000</color>
Run Code Online (Sandbox Code Playgroud)

这足以更改工具栏的颜色。


如果您不想更改整个应用程序的原色(看来这是您最初尝试做的事情),请尝试通过以下方法创建新的工具栏:

将此添加到您应用的build.gradle

compile 'com.android.support:design:23.1.1'
Run Code Online (Sandbox Code Playgroud)

将此添加到您的主布局(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>
Run Code Online (Sandbox Code Playgroud)

设置您的样式(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>
Run Code Online (Sandbox Code Playgroud)

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

Toolbar toolbar = (Toolbar) findViewById(R.id.a_main_toolbar);
setSupportActionBar(toolbar);
Run Code Online (Sandbox Code Playgroud)