Status Bar Color not showing - 5.0 Lollipop Android Studio: (AppCompat-v7:r21)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26522806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:42:02  来源:igfitidea点击:

Status Bar Color not showing - 5.0 Lollipop Android Studio: (AppCompat-v7:r21)

androidandroid-studiostatusbarandroid-5.0-lollipopandroid-appcompat

提问by Adifyr

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

I'm using the AppCompat-v7:21.0.0support library for Android 5.0 Lollipop in Android Studio. My problem is that the Status Bar Color that can be changed by setting colorPrimaryDarkin the values/styles.xmlfile, 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.

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 statusBarColorin styles.xmlbut to no avail. It's still black.

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 statusBarColorin styles.xmlbut 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 minSdkVersionto 21. I tried that, but it didn't work. And I want my app to run on devices with API Level 15 and above.

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

采纳答案by Wayne

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

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

but

android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

must not be set (Source)

must not be set (Source)

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

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

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

回答by Philipp E.

This worked for me:

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

回答by BladeCoder

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.

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.

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

回答by gkiko

Check if you are editing styles.xmlin values-v21folder. If you set SDK version to 21then it won't look for styles.xmlin valuesfolder(but it should do so).

Check if you are editing styles.xmlin values-v21folder. If you set SDK version to 21then it won't look for styles.xmlin valuesfolder(but it should do so).

enter image description here

enter image description here

回答by Tryagain Tsai

test on my nexus7 5.1.1

test on my nexus7 5.1.1

set in style.xml v21/v22 is not work

set in style.xml v21/v22 is not work

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/holo_red_dark</item>

but

but

set in actvivity

set in actvivity

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    getWindow().setStatusBarColor(getResources().getColor(android.R.color.holo_red_dark));
}

is work for me

is work for me

回答by Patrick

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

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.

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

回答by mehulmpt

In my case values-v21/styles.xmlcontained the following line which overrode the status bar color defined in values/style.xmlwith colorPrimaryDark:

In my case values-v21/styles.xmlcontained the following line which overrode the status bar color defined in values/style.xmlwith colorPrimaryDark:

<item name="android:statusBarColor">@android:color/transparent</item>

Removing this worked for me.

Removing this worked for me.

回答by Mwongera808

This worked for me. Removed the status bar color from styles. Add flag and then put the color you want like so

This worked for me. Removed the status bar color from styles. Add flag and then put the color you want like so

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    getWindow().setStatusBarColor(getResources().getColor(R.color.your_color));
}

回答by Adifyr

In v22-appcompatthey (Android) have now added rendering of the status bar color in the android studio preview!

In v22-appcompatthey (Android) have now added rendering of the status bar color in the android studio preview!

About time... Anyways, make sure your appcompatlibrary is updated to the latest version, which is v22.0.+.

About time... Anyways, make sure your appcompatlibrary is updated to the latest version, which is v22.0.+.

Cheers!

Cheers!

回答by Starrover

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

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