[英]I am unable to do dark theme in my app with using forceDarkAllowed
*我使用了 forceDarkAllowed,但它在我的 android 手机上不起作用。 它仍然向我展示一个浅色主题。 我在这里分享了我的 theme.xml 文件。 我正在尝试在不在任何活动中使用任何编程编码的情况下实现深色主题 *
<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>
在应用程序 class 调用 onCreate() 中试试这个 -
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
有关详细信息,请查找 - https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#changing_themes_in-app
原因可能是您的主题继承自Theme.MaterialComponents.DayNight.DarkActionBar
。 尝试改用Theme.MaterialComponents.Light.DarkActionBar
。
根据https://developer.android.com/guide/topics/ui/look-and-feel/darktheme :
如果您的应用使用深色主题(例如 Theme.Material),则不会应用 Force Dark。 同样,如果您的应用程序的主题继承自 DayNight 主题,则不会应用 Force Dark,因为会自动切换主题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.