Change status / notification bar color on api below 21 android?

青春壹個敷衍的年華 提交于 2019-12-12 17:14:17

问题


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>

But it is not working

This is my style code

<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>

回答1:


If you mean the color of AcionBar, you can change programmatically the color with this code:

getSupportActionBar().setBackgroundDrawable(new Color.Drawable(Color.rgb(120,0,47)));

Change numbers to select your own color.




回答2:


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

Hope it works..!!



来源:https://stackoverflow.com/questions/29248542/change-status-notification-bar-color-on-api-below-21-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!