We know that Android 5.0 or more can change the color of the status bar by setting the color Primary Dark. However, most Android developers will encounter the need to make the system status bar transparent or translucent, as shown in the following figure:
Before reading this article, I suggest studying Guo Shen's Android Status Bar Tips to Really Understand Immersion Mode . Here we firmly do not consider the system below 4.4, then basically can be divided into several cases, 4.4, 5.0 above 6.0 below, 6.0 +.
Catalogue of this article:
1. Transparent and translucent status bar
2. Combining Toolbar Skills
3. White background black text status bar
4. Other considerations
Transparent and translucent status bar
First of all, we realize the effect of the top two graphs, pay attention to the judgment of the next version, and go directly to the code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View decorView = getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
getWindow().setStatusBarColor(Color.TRANSPARENT);
//GetWindow (). setStatusBarColor (Color. parseColor (" 40000000"); // This effect is a semi-transparent status bar similar to QQ
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
When the version number is more than 21, the effect is shown in the two figures above, and the 4.4 system is shown in the following figure:
Skills in Combining Title Bars
The above method has basically achieved the effect of transparent status bar, but we add a Toolbar, after setting various attributes, all kinds of elements are moved up, as shown in the following figure:
Some developers use android:fitsSystemWindows = true, and then add a View to the status bar to solve this problem. After my various experiments, I found that this method is not suitable for me.
Here we use a more direct way, setting transparency is equivalent to toolbar has been elongated, so set a padding value to solve the problem. The code is as follows: ScreenUtils is a tool class for acquiring screen attributes:
mToolbar = (Toolbar) findViewById(R.id.toolbar_common);
if (mToolbar != null) {
mToolbar.getLayoutParams().height += ScreenUtils.getStatusHeight(getApplicationContext());
mToolbar.setPadding(0, ScreenUtils.getStatusHeight(getApplicationContext()), 0, 0);
setSupportActionBar(mToolbar);
ActionBar supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setDisplayShowTitleEnabled(false); //This is to not display the default title.
}
}
After setting up, we can achieve the desired effect.
White background black status bar
Sometimes our toolbar background is light or even white. Without modification, the text in the status bar is hard to distinguish because of the default white color. The system above 6.0 provides the method directly, but considering other versions, it needs to be judged concretely:
//Set the status bar text to dark
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//More than 6.0 can be achieved by directly setting the SYSTEM_UI_FLAG_LIGHT_STATUS_BAR attribute.
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(Color.GRAY); //More than 21 do not support 6.0 direct setting method, can be replaced by grey, specific can be set by themselves.
//getWindow().setStatusBarColor(Color.parseColor("#40000000"));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);//Version 4.4 itself contains dark shadows, so it's OK to do nothing else.
}
The results are as follows:
6.0 Effect:
More than 5.0 and less than 6.0:
4.4 Effect:
Matters needing attention
The above method can basically meet most of the requirements, and the effect can be called (semi) transparent status bar. Look below at the real immersive status bar, where text and navigation bars are hidden:
The code is as follows:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
In actual development, of course, we can't write a lot of duplicate code in every Activity, so we suggest that the code of transparent status bar should be encapsulated in BaseActivity, and then the Toolbar should set the same id, or use tags for public use, and a custom layout or other View is the same.
Demo has made some simple encapsulation, you can refer to the specific implementation or need to see the specific requirements.
Address, stamp of all code in this article My Github In the StatusBar package.