更改状态/通知栏颜色在api下面21 android? [英] Change status / notification bar color on api below 21 android?

查看:234
本文介绍了更改状态/通知栏颜色在api下面21 android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到在一些应用程序的状态栏颜色可以更改,并匹配为在api级别21中做了什么。

I have seen in some apps the status bar color could be changed and matched to as what is being done in api level 21.

我搜索并发现此解决方案

I searched and found this solution Source

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">   
<!-- Set AppCompat’s color theming attrs -->
<item name="colorPrimary">@color/my_awesome_red</item>
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
<!-- Other attributes -->
</style>

但不起作用

是我的样式代码

<style name="ToolbarTheme" parent="Theme.AppCompat">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/ColorPrimary</item>
    <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
</style>


推荐答案

StatusBar仅适用于API级别超过21 。如果你的最低API级别小于那个你必须在你的java源文件中做一些改变。转到要向其中添加状态栏颜色的源文件。然后使用注释 @TargetApi(Build.VERSION_CODES.LOLLIPOP)包装 OnCreate()

StatusBar is only for devices with API level more than 21. If your min API level is less than that then you have to do some change in your java source file. Go to the source file to which you want to add the status bar color. Then wrap the OnCreate() method with annotation @TargetApi(Build.VERSION_CODES.LOLLIPOP).

然后在方法中,您必须检查设备是否能够如下所示。

Then inside the method you have to check whether the device is capable or not as shown below.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
 window.setStatusBarColor(getResources().getColor(R.color.Statusbar));
    }

希望它工作..

这篇关于更改状态/通知栏颜色在api下面21 android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆