简体   繁体   English

我无法使用 forceDarkAllowed 在我的应用程序中使用深色主题

[英]I am unable to do dark theme in my app with using forceDarkAllowed

*I have used forceDarkAllowed but it's not working on my android mobile. *我使用了 forceDarkAllowed,但它在我的 android 手机上不起作用。 It's still showing me a light theme.它仍然向我展示一个浅色主题。 I have shared my theme.xml file here.我在这里分享了我的 theme.xml 文件。 I am trying to implement a dark theme without using any programming coding in any activity *我正在尝试在不在任何活动中使用任何编程编码的情况下实现深色主题 *

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_700</item>
    <item name="colorPrimaryVariant">@color/purple_200</item>
    <item name="colorOnPrimary">@color/teal_200</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">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
    <item name="android:textColorSecondary">@color/black</item>
    <item name="android:forceDarkAllowed" tools:ignore="NewApi">true</item>
</style>
<style name="Theme.MyApplication.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>

</style>
<style name="AppTheme">
    <item name="android:windowActionBarOverlay">true</item>
</style>
<style name="Theme.MyApplication.AppBarOverlay">
    <item name="backgroundTint">@color/black</item>
</style>

<style name="Theme.MyApplication.PopupOverlay">
    <item name="backgroundTint">@color/white</item>
    <item name="background">@color/white</item>
    <item name="colorPrimary">@color/white</item>
</style>

<!-- Change tab text appearance -->
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="textAllCaps">true</item>
    <item name="android:textAppearance">@style/CustomTabWidgetText</item>
</style>

<style name="CustomTabWidgetText" parent="@android:style/TextAppearance.Widget.TabWidget">
    <item name="android:textSize">20sp</item>
</style></resources>

Try this in application class call in onCreate()-在应用程序 class 调用 onCreate() 中试试这个 -

 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

and for more details please find - https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#changing_themes_in-app有关详细信息,请查找 - https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#changing_themes_in-app

The reason could be that your theme inherits from Theme.MaterialComponents.DayNight.DarkActionBar .原因可能是您的主题继承自Theme.MaterialComponents.DayNight.DarkActionBar Try to use Theme.MaterialComponents.Light.DarkActionBar instead.尝试改用Theme.MaterialComponents.Light.DarkActionBar

According to https://developer.android.com/guide/topics/ui/look-and-feel/darktheme :根据https://developer.android.com/guide/topics/ui/look-and-feel/darktheme

If your app uses a dark theme (such as Theme.Material), Force Dark will not be applied.如果您的应用使用深色主题(例如 Theme.Material),则不会应用 Force Dark。 Similarly, if your app's theme inherits from a DayNight theme, Force Dark will not be applied, due to the automatic theme switching.同样,如果您的应用程序的主题继承自 DayNight 主题,则不会应用 Force Dark,因为会自动切换主题。

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

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