透明状态栏 - Android 4.4之前(KitKat) [英] Transparent status bar - before Android 4.4 (KitKat)

查看:162
本文介绍了透明状态栏 - Android 4.4之前(KitKat)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 Android 4.4 KitKat(API 19) )可以使状态栏透明。

I know in Android 4.4 KitKat (API 19) it's possible to make the status bar transparent.

但是,例如,Go Launcher Ex和其他人可以选择使其透明,并且它也在预装KitKat,对我来说( Android  4.3 (果冻豆))以及我的Android 4.0(冰淇淋三明治)设备。

But for example, Go Launcher Ex and others have an option to make it transparent, and it's working on pre KitKat also, for me (Android 4.3 (Jelly Bean)) too and also on my Android 4.0 (Ice Cream Sandwich) device.

我不需要root或任何特殊权限来使用它。

I didn't need root or any special privileges to use it.

下载Go Launcher Ex并亲自试用。对我来说,如果我需要root也可以。

Download Go Launcher Ex and try it yourself. For me it would also be OK if I would need root.

但是他们是怎么做到的?
如何在预KitKat设备上使Android的状态栏保持半透明?

But how did they do that? How can I make Android's status bar translucent on pre-KitKat devices?

---澄清事情---

---To clarify things---

我不是在谈论行动栏;我的意思是状态栏(通知栏)!

I'm not talking about the Action Bar; I mean the Status Bar (Notification Bar)!

查看这些示例图片:

(注意,这取自我的股票Galaxy Note 3 Android 4.3 Go Launcher Ex 。)

(Notice, this is taken from my Stock Galaxy Note 3 with Android 4.3 and Go Launcher Ex.)

(同样的事情也适用于我的 Galaxy S2 Android 4.0 。)

(The same thing works with my Galaxy S2 and Android 4.0 too.)

没有透明状态栏:

启用透明状态栏:
(我希望在4.4(API 19)设备上实现此目的)

With transparent status bar enabled: (I want to achieve this on pre 4.4 (API 19) devices)

推荐答案

我发现如何在三星和索尼设备上获得透明状态栏

至少运行Android 4.0。

I discovered how to get a transparent status bar on Samsung and Sony devices
running at least Android 4.0.

首先让我解释一下为什么在大多数设备上都无法实现:

从官方方面看,直到KitKat(4.4)。

但是像三星和索尼这样的设备制造商已在其专有软件中添加了此选项。

To start off let me explain why it isn't possible on most devices:
From the official side of Android, there is no solution until KitKat (4.4).
But some device manufacturers like Samsung and Sony have added this option in their proprietary software.

三星使用他们自己的皮肤版Android,名为TouchWiz,索尼也在使用他们自己的系统。

他们有自己的实现查看类,其中包含额外的 SYSTEM_UI _ 标志,允许在4.4之前的设备上启用透明状态栏!

Samsung uses their own skinned version of Android, called TouchWiz, and Sony is using their own system too.
They have a own implementation of the View class with additional SYSTEM_UI_ flags that allow to enable a transparent statusbar on pre 4.4 devices!

但证明这些标志存在并访问它们的唯一方法是使用Reflection。

这是我的解决方案:

But the only way to prove that those flags are existing and to access them is using Reflection.
Here is my solution:

解析视图标记:

int ResolveTransparentStatusBarFlag()
{
    String[] libs = getPackageManager().getSystemSharedLibraryNames();
    String reflect = null;

    if (libs == null)
        return 0;

    for (String lib : libs)
    {
        if (lib.equals("touchwiz"))
            reflect = "SYSTEM_UI_FLAG_TRANSPARENT_BACKGROUND";
        else if (lib.startsWith("com.sonyericsson.navigationbar"))
            reflect = "SYSTEM_UI_FLAG_TRANSPARENT";
    }

    if (reflect == null)
        return 0;

    try
    {
        Field field = View.class.getField(reflect);
        if (field.getType() == Integer.TYPE)
            return field.getInt(null);
    }
    catch (Exception e)
    {
    }

    return 0;
}

将其应用于 decorView 你的窗口(在任何活动中):

Apply it on the decorView of your Window (inside any Activity):

void ApplyTransparentStatusBar()
{
    Window window = getWindow();
    if (window != null)
    {
        View decor = window.getDecorView();
        if (decor != null)
            decor.setSystemUiVisibility(ResolveTransparentStatusBarFlag());
    }
}

我认为可能有其他设备制造商已经实施这样的旗帜

但我只能算出这两个(因为我拥有三星和索尼设备)

I think there are maybe other device manufacturers that have implemented such a flag
but I was only able to figure this two out (because I own a Samsung and a Sony device)

这篇关于透明状态栏 - Android 4.4之前(KitKat)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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