问题
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.
Thank you in advance.
Edit:
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);
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);
===
}
Case closed..
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>
Styles 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:
<?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>
回答1:
go to your style
add this codes in your theme in style
<item name="colorPrimary">#000000</item>
<item name="colorPrimaryDark">#000000</item>
回答2:
in your parent layout add this code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000"//this is the main code use this line in your code
></LinearLayout>
回答3:
set colorPrimaryDark properly in styles.xml of values-v21 like below -
<style name="AppTheme" parent="Theme.AppCompat.YourTheme">
<item name="android:colorPrimary">@color/PrimaryColor</item>
<item name="android:colorPrimaryDark">@color/PrimaryBackgroundDark</item>
</style>
To change status bar color you have to change - colorPrimaryDark
for app bar you have to change - colorPrimary
回答4:
just edit this
<item name="colorPrimaryDark">#000000</item>
Theme.AppCompat causes "colorPrimaryDark" to be your status bar color by default. Just change that to any value.
"colorPrimary" value changes your toolbar color
hope that solves your problem
回答5:
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
The Problem is with your parent theme in LightTheme, the parent should be Theme.AppCompat.Light.DarkActionBar
LightTheme : colorprimary=orange, colorprimaryDark=DarkOrange.
DarkTheme : colorprimary=orange, colorprimaryDark=Black.
check below code:
<!-- 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>
来源:https://stackoverflow.com/questions/36079983/dark-theme-not-applied-correctly-statusbar-color-unchanged