Android makes the notification bar transparent

  • 2020-12-10 00:49:47
  • OfStack

This feature is supported by andorid4.4 and requires at least api19 to be used, meaning that if Android's machine is below 4.4, the immersion notification bar will not work. The following is a very simple method to use.


 /**
   *  Set the notification bar   This method is in the onCreate() Implementation if it is in the parent class onCreate() Even if all inherit from the parent class, there will be an immersion notification bar. 
   */
public void initSystemBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      setTranslucentStatus(true);
      SystemBarTintManager tintManager = new SystemBarTintManager(this);
      tintManager.setStatusBarTintEnabled(true);
      tintManager.setStatusBarTintResource(R.color.red);
    }
  }
  /**
   *  Sets the status of the notification bar 
   * @param on
   */
  @SuppressLint("InlinedApi") 
  private void setTranslucentStatus(boolean on) { 
    Window win = this.getWindow(); 
    WindowManager.LayoutParams winParams = win.getAttributes(); 
    final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; 
    if (on) { 
      winParams.flags |= bits; 
    } else { 
      winParams.flags &= ~bits; 
    } 
    win.setAttributes(winParams); 
  }

Finally in the layout file add: android:fitsSystemWindows="true"

You can do it.

Android5.0 full transparent status bar effect, the specific example code is as follows:

The code to achieve the above effect is as follows:


public class MainActivity extends Activity {
  @SuppressLint("InlinedApi")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    if(VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
      Window window = getWindow();
      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
          | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
      window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
              | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
              | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      window.setStatusBarColor(Color.TRANSPARENT);
      window.setNavigationBarColor(Color.TRANSPARENT);
    }
    setContentView(R.layout.activity_main);
  }
}

The above code is not good, but also please put forward more heroes, and hope to share this article to help you.


Related articles: