Android development tips: set the color transparency of status bar

Keywords: Android Mobile Google

 

Before Android 4.4, our application couldn't change the color of the status bar of the mobile phone, as shown in the figure above. In order to provide better interface interaction, google started to provide a method to set the immersive status bar in Android 4.4.2. The actual effect is the transparent status bar, and then display our customized color in the position of the status bar, usually For the color of the application's actionbar, or to occupy a picture of the application as a whole in the status bar, many big bulls on the Internet have written the code of the immersion status bar. Today, we introduce a relatively simple and small white transparent setting of the status bar, which is enough to deal with simple problems.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//Set the status bar style you want to modify here
        setContentView(R.layout.activity_main);
    }
}

 

Codes and renderings of full transparency status bar are as follows:
 /**
     * Full penetration status bar
     */

if (Build.VERSION.SDK_INT >= 21) {//21 means 5
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= 19) {//19 means 4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //Virtual keyboard is also transparent
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

 

Translucent status bar codes and renderings are as follows:

  /**
     * Translucent status bar
     */

if (Build.VERSION.SDK_INT >= 21) {//21 means 5
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        } else if (Build.VERSION.SDK_INT >= 19) {//19 means 4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //Virtual keyboard is also transparent
            // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

 

This is done. The user's title bar needs to set padding, otherwise it will be overwritten to the system font above.

Posted by qbox on Tue, 31 Dec 2019 06:47:58 -0800