Whats the right approach to create or change color of a status bar?

坚强是说给别人听的谎言 提交于 2020-02-25 05:11:34

问题


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.

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

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.


回答1:


you can set it in androidMenifest.xml in activity or app theme e.g.

<activity
     android:theme="@style/activityTheme"
     android:name=".MyActivity"
     android:screenOrientation="portrait" />

in style.xml

<style name="activityTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

colorPrimaryDark is status bar color for the activity

if you wanted to change it pro-grammatically you can use bellow code

for Activity

Kotlin
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     window.statusBarColor = ContextCompat.getColor(this, R.color.YOUR_COLOR)
}

Java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.COLOR));
}

for Fragment

Kotlin
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     activity.window.statusBarColor = ContextCompat.getColor(this, R.color.YOUR_COLOR)
}

Java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     getActivity().getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.COLOR));
}



回答2:


manifest

<application android:name="com.smartpos.pocket.app.PocketApplication" android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:allowBackup="true" android:theme="@style/AppTheme">
    <activity android:name="com.smartpos.pocket.activity.impl.StartUpActivity" android:theme="@style/AppTheme">

styles (i have project at api 17)

<style name="AppTheme" parent="AppCompat">
</style>

styles v21

<style name="AppCompat" parent="Theme.AppCompat">
        <item name="colorAccent">@color/color_red_real</item>
        <item name="android:windowAnimationStyle">@null</item>
        <item name="colorPrimary">@color/color_red_real</item>
        <item name="colorPrimaryDark">@color/color_red_real</item>
</style>

status bar does not change anything i thing that device is a laier and there is not android 5.1 or i do not know...




回答3:


hmmm it can be changed... mabie becouse Theme.AppCompat is dark and Theme.AppCompat.Light.DarkActionBar is light

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/color_red_real</item>
    <item name="colorPrimaryDark">@color/color_red_real</item>
    <item name="colorAccent">@color/color_red_real</item>
</style>[![enter image description here][1]][1]

<style name="AppTheme" parent="Theme.AppCompat">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/color_red_real</item>
    <item name="colorPrimaryDark">@color/color_red_real</item>
    <item name="colorAccent">@color/color_red_real</item>
</style>




回答4:


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

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

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

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

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

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>


来源:https://stackoverflow.com/questions/60188515/whats-the-right-approach-to-create-or-change-color-of-a-status-bar

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