简体   繁体   中英

change status bar default color

When I open my application for a second it shows the status bar in blue color, even though, I declared the color to be RED.

In styles:

        <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
                <!-- Customize your theme here. -->
                <item name="colorPrimary">@color/colorPrimary</item>
                <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
                <item name="colorAccent">@color/colorAccent</item>
                <item name="android:statusBarColor">@color/colorPrimaryDark</item>

        </style>
</resources>

In Colors.xml

<resources>
    <color name="colorPrimary">#a20dff</color>
    <color name="colorPrimaryDark">#ff0000</color>
    <color name="colorAccent">#FF4081</color>


</resources>

It's worth mentioning that I change the color grammatically to another color(specified by user) later.But, as in very first second it is different it looks ugly.

Try with adding below line to your v21 theme:

 <item name="android:windowDrawsSystemBarBackgrounds">true</item>

OR

You can do it by programmatically like below:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.BLUE);
}

Implement following method (in your Activity)

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void setStatusBarColor(int statusBarColorResId) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        getWindow().setStatusBarColor(getResources().getColor(statusBarColorResId));

    else Log.e("setStatusBarColor", "Operation not supported");
}

Call it as follows (eg, from onCreate)

setStatusBarColor(R.color.status_bar_color);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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