Status Bar Color not showing - 5.0 Lollipop Androi

2019-01-31 06:06发布

I'm using the AppCompat-v7:21.0.0 support library for Android 5.0 Lollipop in Android Studio. My problem is that the Status Bar Color that can be changed by setting colorPrimaryDark in the values/styles.xml file, is showing up as black, in both the xml layout preview and the emulator.

So what's wrong? Am I missing something? Please let me know. Thanks.

EDIT: I'm aware of the fact that changing the status bar color on Pre-Lollipop versions is not possible. My XML Layout Editor Preview and my Emulator are both set to API Level 21 (5.0 Lollipop). But, the status bar still isn't of the color I set it to in colorPrimaryDark. I tried doing statusBarColor in styles.xml but to no avail. It's still black.

ALSO: I saw one of the answers on a similar question where they advised me to put my minSdkVersion to 21. I tried that, but it didn't work. And I want my app to run on devices with API Level 15 and above.

11条回答
三岁会撩人
2楼-- · 2019-01-31 06:20

Changing the Status Bar color in pre-Lollipop(5.0) is not possible by setting colorPrimaryDark. See this article.

On older platforms, AppCompat emulates the color theming where possible. At the moment this is limited to coloring the action bar and some widgets.

查看更多
ら.Afraid
3楼-- · 2019-01-31 06:21

Please read this: For this to take effect, the window must be drawing the system bar backgrounds with

android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS

but

android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

must not be set (Source)

In case of you don't know how to add that flag:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
查看更多
太酷不给撩
4楼-- · 2019-01-31 06:26

Did you set the target SDK version to 21? I had the same issue when I left the target SDK version to 19. You can leave the min SDK to anything lower.

And of course you need to inherit from the proper theme and make sure your Activity uses it.

查看更多
贪生不怕死
5楼-- · 2019-01-31 06:29

I was working on an old app and trying to convert it to material style. The code and everything was fine however the only mistake I had that was hindering the status bar tinting on >= Lollipop devices was "TargetSDKVersion" in build.gradle. It was set to less then 21. I changed it to 21 and statusbar tinting started working.

查看更多
趁早两清
6楼-- · 2019-01-31 06:30

This worked for me:

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.some_color));
    }
查看更多
Viruses.
7楼-- · 2019-01-31 06:30

In my case, the culprit was jfeinstein10/SlidingMenu library. I replaced the library with Android navigation drawer and now it displays status bar color correctly.

查看更多
登录 后发表回答