How to set status bar tint color in layout/style XML?

牧云@^-^@ 提交于 2019-12-21 06:25:19

问题


I'm using the windowTranslucentStatus in my app theme.

Now, also want to tint the status bar to match my ActionBar.

Currently I'm using the SystemBarTint library, which is mostly fine except of one problem:

When the application launches, the OS shows a themed mockup layout as a splash image during the actual inflation. At that stage, there is no status bar tinting yet.

It looks like that (Took me 5 times to capture this screenshot during app launch):

Only after the activity is fully loaded the tint is applied. This is a problem for me and I'm looking for a way to somehow show the full branding on the status bar during launch.

I have read This blog post Which explains how you can actually control what will be displayed during launch by specifying a windowBackground.

I set the windowBackground to my main color -> then the entire screen painted pink.

I set background property on my activity's root element to white, but then again during launch the screen is pink, and only after the activity is loaded the content's background clears to white. This OK but not ideal.

I know I can create a bitmap with a white background and a color where the status bar should be, but then it won't be theme-able, meaning I'll have to create a new bitmap if the main color changes.

So to sum it up my question is this: Do you know a way to set a tint color for the status bar (in KitKat) in XML?

Alternatively, is it possible to create an XML drawable that will contain a colored bar at the top with a fixed height?


回答1:


Here it is: The (almost) perfect answer

Good to know that someone else was struggling the exact same problem like me.

To make it simple I'll post here the XML drawable:

window_background.xml:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/status_bar_background"
          android:drawable="@color/main_app_color"/>

    <item android:id="@+id/content_window_background"
        android:drawable="@color/white"
        android:top="25dp"/>
</layer-list>

And now in my theme I assign this drawable as the windowBackground:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="BaseAppTheme">
        <!-- Customize your theme here. -->
        <item name="android:windowTranslucentNavigation">@bool/translucent_nav_bar</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowBackground">@drawable/window_background</item>
    </style>
</resources>



回答2:


As I have replied here, you can see a full working example I created here (unfortunately too long to copy, but rest assured that repo is going nowhere!).

Oh and from Lollipop onwards there won't be any need for tricks ;)



来源:https://stackoverflow.com/questions/26402466/how-to-set-status-bar-tint-color-in-layout-style-xml

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