【问题标题】:Android Studio Dark Theme FunctionAndroid Studio 深色主题功能
【发布时间】:2022-06-10 18:04:41
【问题描述】:

暗/亮模式功能有问题。当我想打开暗模式主题时,它似乎在工具栏中写了我的应用程序名称,这是我不想要的。例如:

应该是这样的: 我不知道为什么我打开暗模式主题时会出现这个问题。

  This is my code: 

package com.example.myapplication;

import android.os.Bundle;


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

public class SettingsFragment extends PreferenceFragmentCompat {


    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);


        ListPreference themePreference=findPreference("theme");
        assert themePreference != null;
        themePreference.setOnPreferenceChangeListener((preference, newValue) -> {
            if(newValue.equals("light_mode"))
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

            if(newValue.equals("dark_mode"))
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            if(newValue.equals("system_default"))
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);

            return true;
        });


    }
} 

我不知道它是否有帮助,但这里是themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.Light" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">@color/black</item>
        <!-- Customize your theme here. -->


    </style>







</resources>                                                                                                                                                            
                                                                               
 Here is theme.xml/night:



 <resources xmlns:tools="http://schemas.android.com/tools">
        <!-- Base application theme. -->
        <style name="Theme.Dark" parent="Theme.AppCompat.DayNight.NoActionBar">
            <!-- Primary brand color. -->
            <item name="colorPrimary">@color/purple_200</item>
            <item name="colorPrimaryVariant">@color/purple_700</item>
            <item name="colorOnPrimary">@color/black</item>
            <!-- Secondary brand color. -->
            <item name="colorSecondary">@color/teal_200</item>
            <item name="colorSecondaryVariant">@color/teal_200</item>
            <item name="colorOnSecondary">@color/black</item>
            <!-- Status bar color. -->
            <item name="android:statusBarColor" tools:targetApi="l">@color/black</item>
    
    
    
    
            <!-- Customize your theme here. -->
    
    
    
        </style>
    
    </resources> 

                                                                                                                                                                                              

【问题讨论】:

  • 您能分享您的布局和主题以更好地理解问题吗?
  • 问题是主题是在你的下面添加默认的Toolbar。确保在您的活动主题中包含android:windowNoTitle=true/android:windowActionBar=false(浅色和深色)
  • @MatPag 我到处都写了这段代码,那个工具栏还在。我不知道我错过了什么。

标签: java android fragment android-dark-theme


【解决方案1】:

而不是 android:windowNoTitle=true/android:windowActionBar=false

使用

windowNoTitle=true
windowActionBar=false

即没有android前缀

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 2020-01-15
    相关资源
    最近更新 更多