An overview of the
Sometimes we want to achieve an effect across the top status bar on android phones, such as an image being displayed directly in the status bar. Here’s an example:
\
There is an image on this page, and the image is displayed at the top of the entire page. The status bar is floating on this image.
Methods to implement a transparent status bar
Android 4.4 +
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = activity.getWindow();
// Translucent status bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// Translucent navigation bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
Copy the code
Android 5.0+ adaptation methods:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.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);
}
Copy the code
How to use
Call it before the setContentView method in the activity’s onCreate method. Such as:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GetWindow (). RequestFeature (Window. FEATURE_NO_TITLE); . / / here call StatusbarUtils enableTranslucentStatusbar (this); setContentView(R.layout.activity_main); }Copy the code
In the above code I use the helper class StatusbarUtils, which calls the concrete methods that implement the transparent status bar. The complete code for this class is as follows:
package zhangyf.vir56k.translucentbardemo; import android.app.Activity; import android.graphics.Color; import android.os.Build; import android.view.View; import android.view.Window; import android.view.WindowManager; /** * Created by Zhangyunfei on 16/6/17. */ public class StatusbarUtils {/** * enable transparent status bar * @param activity */ public static void enableTranslucentStatusbar(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = activity.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); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); // Translucent status bar window.setFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // Translucent navigation bar window.setFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); }}}Copy the code
Android: fitsSystemWindows label
Android provides android: fitsSystemWindows label, to fit the form, we can actually use it in the layout of our view. It causes the specified view to automatically increase the “space above the height of the status bar”, that is, it ensures that your view is below the status bar, not below it. android:fitsSystemWindows=”true”
Sample source code download
Github.com/vir56k/demo…