Java 透明状态栏 - Android 4.4 (KitKat) 之前
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21865621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transparent status bar - before Android 4.4 (KitKat)
提问by bricklore
I know in Android 4.4KitKat (API 19) it's possible to make the status bar transparent.
我知道在Android 4.4KitKat (API 19) 中可以使状态栏透明。
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.
但是,例如,Go Launcher Ex 和其他人可以选择使其透明,并且它也可以在 KitKat 之前运行,对我来说也是如此(Android 4.3(Jelly Bean))以及我的 Android 4.0(冰淇淋三明治)设备。
I didn't need root or any special privileges to use it.
我不需要 root 或任何特殊权限来使用它。
Download Go Launcher Ex and try it yourself. For me it would also be OK if I would need root.
下载 Go Launcher Ex 并亲自尝试。对我来说,如果我需要 root 也可以。
But how did they do that? How can I make Android's status bar translucent on pre-KitKat devices?
但是他们是怎么做到的呢?如何在 KitKat 之前的设备上使 Android 的状态栏半透明?
---To clarify things---
---澄清事情---
I'm not talking about the Action Bar; I mean the Status Bar(Notification Bar)!
see these example pictures:
请参阅这些示例图片:
(Notice, this is taken from my Stock Galaxy Note 3with Android 4.3and Go Launcher Ex.)
(请注意,这是从我的带有Android 4.3和Go Launcher Ex 的Stock Galaxy Note 3 中获取的。)
(The same thing works with my Galaxy S2and Android 4.0too.)
(同样的事情也适用于我的Galaxy S2和Android 4.0。)
Withouta transparent status bar:
没有透明状态栏:
Withtransparent status bar enabled: (I want to achieve this on pre 4.4 (API 19) devices)
随着启用透明状态栏:(我想要实现这个预先4.4(API 19)设备)
采纳答案by bricklore
I discovered how to get a transparent status bar on Samsung and Sony devices
running at least Android 4.0.
我发现了如何
在至少运行 Android 4.0 的三星和索尼设备上获得透明状态栏。
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 的官方方面来看,直到 KitKat (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!
三星使用他们自己的 Android 皮肤版本,称为 TouchWiz,索尼也在使用他们自己的系统。
他们有一个自己的View
类实现,带有额外的SYSTEM_UI_
标志,允许在 4.4 之前的设备上启用透明状态栏!
But the only way to prove that those flags are existing and to access them is using Reflection.
Here is my solution:
但是证明这些标志存在并访问它们的唯一方法是使用反射。
这是我的解决方案:
Resolve the view flag:
解决视图标志:
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;
}
Apply it on the decorView
of your Window
(inside any Activity):
将其应用于decorView
您的Window
(在任何活动中):
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)
我认为可能有其他设备制造商已经实施了这样的标志,
但我只能弄清楚这两个(因为我拥有三星和索尼设备)
回答by Ali Bagheri
Use this. :)
用这个。:)
after super() and before set content.
在 super() 之后和设置内容之前。
if (Build.VERSION.SDK_INT >= 19)
{
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21)
{
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true);
}
if (Build.VERSION.SDK_INT >= 21)
{
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
public static void setWindowFlag(Activity activity, final int bits, boolean state)
{
Window win = activity.getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
if (state)
{
winParams.flags |= bits;
}
else
{
winParams.flags &= ~bits;
}
}