问题描述
我正在尝试为我的应用程序创建一个 light 和 dark 主题.当应用浅色主题时,状态栏应为橙色,但是一旦我切换到深色主题,状态栏将保持橙色,尽管我希望它变为黑色.
I am trying to create a light and dark theme for my application.When applying the light theme the statusbar is orange as it should be but as soon as I switch to the dark theme the statusbar stays orange although I want it to be black.
我不是主题创作方面的专家,因此我们非常感谢您的帮助.
I am not a pro in theming so any help is really appreciated.
我提供了一些屏幕截图,所以您可以了解我的意思.
I included some screenshots, so you can see what I mean.
谢谢.
我自己找到了一个解决方案(应该有人遇到同样的问题),在我的Loginactivity的开头,我检查了通过SharedPrefs文件应用了哪个主题.
I found a solution myself (should somebody else have the same problem), in the beginning of my Loginactivity I check which theme is applied through a SharedPrefs file.
// which theme is set.
SharedPreferences settings = getSharedPreferences(Helper.PREF_NAME, MODE_PRIVATE);
Helper.newTheme = settings.getInt("themeCustom", 0);
如果设置了黑色主题,那么我自己使用WindowManager修改状态栏:
If the black theme is set, then I just modify the statusbar myself with the WindowManager:
if (Helper.newTheme == Helper.THEME_DARK) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor("#1B1C1C"));
}
this.setTheme(R.style.DarkTheme);
===
}
案例已关闭.
Styles.xml:
Styles.xml:
<resources>
<!-- reference to CardView White/Dark styles -->
<attr name="cardStyle" format="reference" />
<attr name="txtBgStyle" format="reference" />
<!-- Light application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#FD8300</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#F59F00</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#FF4081</item>
<item name="android:windowDisablePreview">true</item>
<!-- v7.widget.CardView background color -->
<item name="cardStyle">@style/CardView.Light</item>
<item name="txtBgStyle">@style/CardView.Light</item>
</style>
<!-- Dark application theme. -->
<style name="DarkTheme" parent="Theme.AppCompat">
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#FD8300</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#1B1C1C</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#FAFAFA</item>
<!-- v7.widget.CardView background color -->
<item name="cardStyle">@style/cardStyle</item>
<item name="txtBgStyle">@style/txtBgStyle</item>
</style>
<!-- v7.widget.CardView dark style -->
<style name="cardStyle">
<!-- Card background color -->
<item name="cardBackgroundColor">#282929</item>
</style>
<!-- Custom dark style for textviews, layouts, etc -->
<style name="txtBgStyle">
<item name="android:background">#282929</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
样式v21:
<resources>>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
AndroidManifest.xml:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myapp.example.com.myapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:noHistory="false"
android:label="@string/title_activity_login"/>
</application>
</manifest>
推荐答案
此代码将根据需要更改主题.您应同时设置 colorprimary
和 colorprimaryDark
并在styles.xml文件中进行编辑.
This code will change the themes as you want.You should make both colorprimary
and colorprimaryDark
override and edit in your styles.xml file.
更新
Update
问题出在您在LightTheme中的父主题上,父主题应该是 Theme.AppCompat.Light.DarkActionBar
The Problem is with your parent theme in LightTheme, the parent should be Theme.AppCompat.Light.DarkActionBar
LightTheme :colorprimary =橙色,colorprimaryDark = DarkOrange.
LightTheme : colorprimary=orange, colorprimaryDark=DarkOrange.
DarkTheme :colorprimary =橙色,colorprimaryDark =黑色.
DarkTheme : colorprimary=orange, colorprimaryDark=Black.
检查以下代码:
<!-- Light application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#FD8300</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#F59F00</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#FF4081</item>
<item name="android:windowDisablePreview">true</item>
<!-- v7.widget.CardView background color -->
<item name="cardStyle">@style/CardView.Light</item>
<item name="txtBgStyle">@style/CardView.Light</item>
</style>
<!-- Dark application theme. -->
<style name="DarkTheme" parent="Theme.AppCompat">
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#FD8300</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#1B1C1C</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">#FAFAFA</item>
<!-- v7.widget.CardView background color -->
<item name="cardStyle">@style/cardStyle</item>
<item name="txtBgStyle">@style/txtBgStyle</item>
</style>
<style>
这篇关于深色主题未正确应用,状态栏颜色未更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!