创建或更改状态栏颜色的正确方法是什么?

问题描述:

我在更改状态栏的颜色方面遇到了一个大问题.我正在使用的设备告诉我 android 5.1 但像 windowTranslucentStatusandroid:colorPrimaryDarkandroid:statusBarColor 这样的工作人员在全部(半透明有 20% 的暗透明度),其他任何东西都不起作用.

I have a quiet big problem with changing the color of my status bar. The device on which I working on is telling me android 5.1 but staff like windowTranslucentStatus or android:colorPrimaryDark or android:statusBarColor just does not work at all (translucent have like 20% dark transparence) and anything else does not work.

所以它似乎是 android 5.0 下的东西.

so it seems like it is something under android 5.0.

所以我尝试了一些应用程序 - 一个确实有效,因为它们在旧应用程序前面制作了自己的状态栏.但第一件事我不会使用不同的应用程序,第二件事这个应用程序不能做我在我的项目中使用的动画通知

So I tried some applications - one did work because they made their own status bar in front of old ones. but the first thing I do not won't use the different app and second thing this app can not do animation notification which I using in my project

所以我在状态栏前面制作了一个带有我的颜色的按钮,但现在我需要在那里添加所有图标.(我只需要 wifi、蓝牙、电池、时间.)就是这样,但我发现这很难.

so I made a button with my color in front of my status bar but now I need to add there all icons. (i need there only wifi, bluetooth, battery, time.) that is it but I found that quite hard.

所以现在我有点卡住了有没有人知道如何更改该颜色或如何以某种方式复制通知图标?我不需要滑动通知栏,我只需要看到它.

so now I am little bit stuck does anyone know hack how to change that color or how to somehow replicate notification icons? I do not need to slide notification bar I just need that to be seen.

所以最后一件事是它确实有效,这是任何一个不会留下状态栏阴影的代码......不知道为什么,但这是解决方案我...我将我的背景样式设为红色...并且在所有屏幕(布局)上我添加了从边缘到边缘的所有布局...所以我的背景现在是这个布局...真正的背景仅用于状态酒吧...

So last thing yes it does works this is ony one code whitch does not leave behind status bar shadow... do not know why but yes this was solution for me... i made in styles my background red... and on all screens (layouts) i add behind everything layout from edge to edge... so my background is now this layout... and real background is only for status bar...

像 TranslucentStatus 这样的解决方案和我的颜色的小布局确实在状态栏后面留下了阴影,所以我永远无法获得我需要的那种颜色......而像 statusbarcolor 或 primarydarkcolor 这样的解决方案以编程方式或在样式中从未奏效......所以如果你有这个问题你可以做这个丑陋的解决方案......但它绝对有效......

solution like TranslucentStatus and little layout with my color did left shadow behind status bar so i never could get exactly that color which i needed... and solutions like statusbarcolor or primarydarkcolor programaticly or at styles never worked... so if you have this problem you can do this ugly solution... but it definitly work...

非常感谢@Muhammad Muzammil Sharif 没有他,我永远不会走到这一步......(我公司的 3 个人整个星期都在尝试这样做)

big thanks to @Muhammad Muzammil Sharif without him i will never get to this point... (3 people at my company tryed to do that whole week)

这是最终版本...现在我想隐藏一些系统通知...但我不认为这是可能的...再次感谢大家

this is final wersion... now i would like to hide some of thoes system notifications... but i do not thing that is possible... thanks again to everyone

所以在你所有的 Java 活动中你都需要这个:

so in all of your java activity you need this:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }

在你的styles.xml中你需要设置背景颜色(这个颜色就是你的状态栏颜色):

and at your styles.xml you need to set background color (this color will be your status bar color):

<style name="AppCompat" parent="Theme.AppCompat">
    <item name="android:colorBackground">@color/YOUR_STATUS_BAR_COLOR</item>
</style>

并且在所有布局中,您需要添加布局作为您的背景:

and at all your layouts you need to add layout which will be your background:

<LinearLayout
    android:background="@color/YOUR_BACKGROUND_COLOR"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    />

当然,如果你想使用@color/just name...你需要在colors.xml中设置:

of course if you would like use @color/just name... you need to set that at colors.xml:

<color name="YOUR_STATUS_BAR_COLOR">#cf031c</color>
<color name="YOUR_BACKGROUND_COLOR">#383838</color>