简体   繁体   English

如何更改android状态栏的背景颜色

[英]How to change the background color of android status bar

I want to change the background color of the status bar by writing an application. 我想通过编写应用程序来更改状态栏的背景颜色。 My android device has black color, I want to change it to some other color. 我的Android设备有黑色,我想将其改为其他颜色。 I saw some posts related to it here, but they are telling about notification background. 我在这里看到了一些与之相关的帖子,但是他们正在讲述通知背景。

If any body knows about this please help me. 如果有人知道这个,请帮助我。

The default status bar 默认状态栏

在此输入图像描述

After using a drawable as background to status bar 使用drawable作为状态栏的背景

在此输入图像描述

Sorry, unless you are making a custom ROM this isn't possible, unless you only want the status bar changed for your app. 抱歉,除非您正在制作自定义ROM,否则这是不可能的,除非您只想为您的应用更改状态栏。

This would require a heck of a lot of work. 这需要大量的工作。

First you will need to add Theme.NoTitleBar.Fullscreen to your manifest 首先,您需要将Theme.NoTitleBar.Fullscreen添加到清单中

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        >

Then once you have done that you need to create a standard layout which represents the status bar, this would mean that you have to add the time, and also receive all the notifications from other apps, I do not personally know how to do that but I'm sure there is a way. 然后,一旦你完成了,你需要创建一个代表状态栏的标准布局,这意味着你必须添加时间,并且还接收来自其他应用程序的所有通知,我个人不知道该怎么做但是我确信有办法。

If you really want to do this goodluck, you have a hard time ahead of you. 如果你真的想要做这个好运,那么你将面临困难。


Sorry, unless you have the knowledge how to build custom ROMS I do not think this is possible 对不起,除非您知道如何构建自定义ROMS,否则我认为这是不可能的

This is possible on Kitkat and after. 这可以在Kitkat和之后进行。

If you want to use it in an application (like you asked), you could use this library https://github.com/jgilfelt/SystemBarTint 如果你想在一个应用程序中使用它(就像你问的那样),你可以使用这个库https://github.com/jgilfelt/SystemBarTint

You only need to write: 你只需要写:

// set a custom tint color for all system bars
tintManager.setTintColor(Color.parseColor("#99000FF"));   
// set a custom navigation bar resource
tintManager.setNavigationBarTintResource(R.drawable.my_tint);
// set a custom status bar drawable
tintManager.setStatusBarTintDrawable(MyDrawable);

In styles.xml do this: 在styles.xml中执行以下操作:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/color_primary</item>
        <item name="colorPrimaryDark">@color/color_secondary</item>
        <item name="colorAccent">@color/color_accent</item>
         <!---Below is the code for status bar color------>
        <item name="android:statusBarColor">@color/color_primary</item>
    </style>
</resources>

Place this is in your values-v21/styles.xml, to enable this on Lollipop and later. 将它放在您的values-v21 / styles.xml中,以便在Lollipop及更高版本上启用它。

In order to do it programmatically you can do this: 为了以编程方式执行此操作,您可以执行以下操作:

Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(activity.getResources().getColor(R.color.example_color));

IF you want to update the status bar color on Lollipop without upgrading your ADT and SDK and all that related stuff, you can use reflections to reach the methods of API 21 (Lollipop) and higher 如果你想更新Lollipop上的状态栏颜色而不升级你的ADT和SDK以及所有相关的东西,你可以使用反射来达到API 21(Lollipop)及更高版本的方法

in your activity : 在你的活动中:

    if (Build.VERSION.SDK_INT >= 21) {
        Window window = getWindow();

        // original code, works on Lollipop SDKs
        // window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        // window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        // window.setStatusBarColor(getResources().getColor(YOUR_COLOR));

        try {
            // to work on old SDKs 
            int FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS = 0x80000000;
            window.addFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            Class<?> cls = window.getClass();
            Method method = cls.getDeclaredMethod("setStatusBarColor",
                    new Class<?>[] { Integer.TYPE });

            method.invoke(window, Res.color(theme.statusColor));

        } catch (Exception e) {
            // upgrade your SDK and ADT :D
        }

    }

my current minimum API is 15, if you cannot find 我目前的最低API是15,如果你找不到的话

WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

in your SDK, you can get it's value from documentation as what i did with 在你的SDK中,你可以从文档中获得它的价值

WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS

hope this helped 希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM