How to set Status bar to white background and blac

2019-02-26 02:59发布

问题:

I want to set the status bar to white background and black text(and black icon) in my app. I found some apps can do this. But search from google, I can not found any solution to do this.There is a lot solutions about how to set status bar's color.Also like SystemBarTint ,It just can set background, but not set the whole status bar's style(white background and black text OR black background and white text color).

回答1:

Place this is your values-v21/styles.xml, to enable this on Lollipop: It will only work above API 21

  <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>
            <item name="android:statusBarColor">@color/color_primary</item>
        </style>
    </resources>


回答2:

Its for API 21 Or greator

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.setStatusBarColor(getResources()
                        .getColor(R.color.YOurColorname));
            }


回答3:

I found this solution use this code:

  getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
  getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

getWindow().setStatusBarColor(Color.TRANSPARENT);// SDK21