我如何使状态栏白色与黑色图标?

我想改变我的应用程序状态栏的颜色,使其白色与黑色图标(而不是默认的黑色与白色图标)。有什么办法吗?

95048 次浏览

No such way to this unless if you have a control of the whole rom to customize that manually. What i suggest you to do is, use a light gray color for the status bar color through your theme like the google drive does.

Edit: please refer to @Wrekcker answer as this changed in android M.

With Android M (api level 23) you can achieve this from theme with android:windowLightStatusBar attribute.

Edit :

Just as Pdroid mentioned, this can also be achieved programatically:

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

I had the same issue.

I noticed that when I use normal layout like RelativeLayout it doesn't work. But when I switched to that come from support library like CordinatorLayout it has finally started to work. Try this.

  <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Activities.Activity">




<android.support.v7.widget.Toolbar
android:id="@+id/tb_activity"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />




</android.support.design.widget.CoordinatorLayout>

<********* BELOW OTHERS ATTRIBUTES ************>

styles

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


<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>


<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />


<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

And in the manifest

<activity
android:name=".Activities.MyActivity"
android:label="Activity"
android:theme="@style/AppTheme.NoActionBar">
</activity>

It is possible to make the white status bar with grey icons e.g. this way for SDK >= 23 (see docs):

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowLightStatusBar">true</item>
</style>

in your styles.xml and set the colorPrimary to white or programmatically:

getWindow().setStatusBarColor(Color.WHITE);

Just added to my activity on Kotlin:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
window.decorView.systemUiVisibility =View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
window.statusBarColor = Color.WHITE
}

just add this to you style

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>

set android:windowDrawsSystemBarBackgrounds to true*. This is a flag whose description is given below:

Flag indicating whether this Window is responsible for drawing the background for the system bars. If true and the window is not floating, the system bars are drawn with a transparent background and the corresponding areas in this window are filled with the colors specified in {@link android.R.attr#statusBarColor} and {@link android.R.attr#navigationBarColor}. Corresponds to {@link android.view.WindowManager.LayoutParams#FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS}.

You can use it this way in Kotlin

 window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR

Make sure to call it in onCreate() before setContentView()

I just used this

  1. Set the parent in style
parent="Theme.AppCompat.Light.NoActionBar"
  1. Add this to the menu bar in xml
  app:theme="@style/AppThemeWithoutActionBarwithwhitehed"
android:background="#fff"

this is what worked for me

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowLightStatusBar">true</item>
<item name="android:statusBarColor">@android:color/white</item>
</style>

I achieved it using:

  if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true);
}
if (Build.VERSION.SDK_INT >= 19) {
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);     }


if (Build.VERSION.SDK_INT >= 21) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
getWindow().setStatusBarColor(Color.WHITE);
}






setContentView(R.layout.activity_mainss);

Theme is set in AndroidManifest:

 <style name="Theme.MapRiti" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryDark">@color/orange</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
</style>

one of the best solutions you can use:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val decor = this.window.decorView
// valu reflected to set status bar icons in black
if (value) {
decor.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
} else {
// We want to change tint color to white again.
// You can also record the flags in advance so that you can turn UI back completely if
// you have set other flags before, such as translucent or full screen.
decor.systemUiVisibility = 0
}
}

this code is inspired by this comment https://stackoverflow.com/a/37694455/10277217

In your app theme use color for status bar like this @color/statusBar color is in color file ?attr/statusBarBackground this code is for setting status bar color automatically when light mode flag is called..

true and this code of flag is for changing the icon's color to dark when light mode flag is called

this thing worked for me

 In your app theme use color for status bar like this
<style name="Theme.YourAppName"
parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/statusBar</item>
<item name="statusBarBackground">@color/statusBar</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->




<item name="android:statusBarColor" tools:targetApi="l">? attr/statusBarBackground</item>


use this code for icon change color
<item name="android:windowLightStatusBar">true</item>
<!-- Customize your theme here. -->
</style>

There are two approaches we can take.

1. Dynamically:

fun Activity.setStatusBarColorAndAppearance(statusBarColor: String, isLight: Boolean ) {
try {
window.statusBarColor = (Color.parseColor(statusBarColor))// Or we can use from resource color:  ContextCompat.getColor(mContext, R.color.colorPrimary)
WindowInsetsControllerCompat(window, window.decorView).isAppearanceLightStatusBars = isLight
} catch (e: Exception) {
e.printStackTrace()
}
}

And use it inside the onCreate() method:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)


setStatusBarColorAndAppearance("#5e5c64", false)


....


}

2. By xml:

Use this in AppTheme

<item name="android:windowLightStatusBar">false</item>
<item name="android:statusBarColor">@color/colorPrimary</item>

Note: Tested above 8.0.