简体   繁体   中英

Android status bar color not changing

在此处输入图片说明 I have an Activity and a Fragment in my project.In the Activity I set the layout take the entire screen and in fragment I try to change the status bar color programmatically. The status bar is shown over the layout but the color is not changed.

Activity:

if (Build.VERSION.SDK_INT > 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);

        }

Fragment:

 if (Build.VERSION.SDK_INT > 16) {
            getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            if (Build.VERSION.SDK_INT >= 21) {
                Log.e("statusbarcolor","changing..");
                getActivity().getWindow().setStatusBarColor(ContextCompat.getColor(getContext(),R.color.statusBarColor));
            }
        }

I guess , Problem is your Build.VERSION.SDK_INT section .

In my case (Activity)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        Window window = activity.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.parseColor("#54d66a");
    }

Just try doing:

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);
}

Just tested this with ActionBarActivity and it works alright.

Note: Setting the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag programmatically is not necessary if your values-v21 styles file has it set already, via:

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

Create a method in your activity

public void updateStatusBarColor(String color){// Color must be in hexadecimal fromat
 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(color));
    }
}

Call this method from your fragment

((ActivityName)getActivity()).updateStatusBarColor("#000000")

In given below using my application :

..................
 switch (position) {
        case 0 :
            fragment = new SampleOneFragment();
            color = R.color.ColorPrimaryDark;
            break;
        case 1:
            fragment = new SampleOneFragment();
            color = R.color.AlertRed_dark;
            break;
        case 2:
            fragment = new SampleOneFragment();
            color = R.color.ProgressBarPurple_dark;
            break;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(ContextCompat.getColor(this, color));
    }  
    replaceFragment(fragment);
}

我终于做到了,我只是在片段中使用了以下代码:

getActivity().getWindow().clearFlags(WindowManager.FLAG_FULL_SCREEN);

User this code

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {

            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.my_statusbar_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