将StatusBar更改为White

时间:2018-10-10 20:12:01

标签: java android statusbar

我试图仅在一项活动中将状态栏颜色更改为白色,但是图标几乎没有出现:

enter image description here

我正在使用以下代码:

 Window window = getWindow();
 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
 window.setStatusBarColor(Color.TRANSPARENT);

我无法更改ColorPrimaryDark,因为我已经为所有应用设置了一种颜色。

谢谢。

编辑

我在评论初学者错误。这有一个非常简单的解决方案,只需在清单文件中为此活动设置一个主题:

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

关于我的Activity的这段Java代码,如@shahab所说:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    window.setStatusBarColor(Color.TRANSPARENT);
}

3 个答案:

答案 0 :(得分:3)

更改状态栏颜色仅适用于棒棒糖上方的android

1。您可以通过此行以编程方式更改状态栏的颜色:

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

2。您可以使用平滑的过渡动画来做到这一点:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    int startColor = getWindow().getStatusBarColor();
    int endColor = ContextCompat.getColor(context, R.color.your_color);
    ObjectAnimator.ofArgb(getWindow(), "statusBarColor", startColor, endColor).start();
}

3。或您可以将其添加到values / styles.xml文件中的主题样式。项目colorPrimaryDark将用于您的应用状态栏颜色

<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

但要将状态栏图标颜色更改为深色,您可以使用SYSTEM_UI_FLAG_LIGHT_STATUS_BAR标志,该标志可用于M以上的android

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}

或将其添加到主题样式xml中:

<item name="android:windowLightStatusBar">true</item>

答案 1 :(得分:2)

onCreate()方法中使用它:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

这可从API 23+开始使用。

答案 2 :(得分:0)

Window window = getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    window.setStatusBarColor(Color.TRANSPARENT);
}

else {
    window.setStatusBarColor(Color.WHITE);
}