How to achieve it? 1.) First realize full screen
The first type: inherit the topic specific topic
On Android API 19 or above, you can use the topics related to ****.TranslucentDecor***, which comes with corresponding translucent effects, and the two themes are newly added, so you need to create a new values-v19 folder and create a styles file and add the following code
<style name="AppBaseTheme" parent="android:"> <!-- Customize your theme here. --> </style>
The second type: the way to use code in activity
Android 4.4 or above can add the following code
if (.SDK_INT >= Build.VERSION_CODES.KITKAT) { //Transparent status bar(.FLAG_TRANSLUCENT_STATUS); //Transparent navigation bar(.FLAG_TRANSLUCENT_NAVIGATION); }
Android 5.0 or above can also use the following code to achieve full screen
if (.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { (.FLAG_TRANSLUCENT_STATUS | .FLAG_TRANSLUCENT_NAVIGATION); ().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); (.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); }
2.) Solve the status bar placeholding problem
First: Add the following settings to the theme
<item name="android:fitsSystemWindows">true</item>
The second type: add the following code in the root directory of activity layout
android:fitsSystemWindows="true"
The third type: Setting through Java code
(true);
3.) Status bar navigation bar set background color
For 4.4 or above, you can use modify the background color of the contentView, or dynamically add a view to the contentView.
if (.SDK_INT >= Build.VERSION_CODES.KITKAT) { //Transparent status bar(.FLAG_TRANSLUCENT_STATUS); //Transparent navigation bar(.FLAG_TRANSLUCENT_NAVIGATION); //Set contentview to fitsSystemWindowsViewGroup contentView = (ViewGroup) findViewById(); View childAt = (0); if (childAt != null) { (true); } //Color statusbarView view = new View(this); (new (.MATCH_PARENT, getStatusBarHeight(this))); (color); (view); }
Dynamically obtain the StatusBarHeight function as follows
/** * Get the status bar height * * @param context context * @return Status bar height */ private static int getStatusBarHeight(Context context) { // Get the status bar height int resourceId = ().getIdentifier("status_bar_height", "dimen", "android"); return ().getDimensionPixelSize(resourceId); }
Dynamically obtain the NavigationBarHeight function as follows
/** * Get navigation bar height * * @param context context * @return Navigation bar height */ public static int getNavigationBarHeight(Context context) { int resourceId = ().getIdentifier("navigation_bar_height", "dimen", "android"); return ().getDimensionPixelSize(resourceId); }
4.) Post the overall Java code implementation method
private void initWindows() { Window window = getWindow(); int color = getResources().getColor(); if (.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { (.FLAG_TRANSLUCENT_STATUS | .FLAG_TRANSLUCENT_NAVIGATION); ().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); (.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); //Set the status bar color (color); //Set the navigation bar color (getResources().getColor()); ViewGroup contentView = ((ViewGroup) findViewById()); View childAt = (0); if (childAt != null) { (true); } } else if (.SDK_INT >= Build.VERSION_CODES.KITKAT) { //Transparent status bar (.FLAG_TRANSLUCENT_STATUS); //Transparent navigation bar (.FLAG_TRANSLUCENT_NAVIGATION); //Set contentview to fitsSystemWindows ViewGroup contentView = (ViewGroup) findViewById(); View childAt = (0); if (childAt != null) { (true); } //Color statusbar View view = new View(this); (new (.MATCH_PARENT, getStatusBarHeight(this))); (color); (view); } if (.SDK_INT >= Build.VERSION_CODES.M && useStatusBarColor) {//After Android 6.0, you can modify the text color and icon of the status bar getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } }
This is the article about the effect of Android Studio full-screen immersive transparent status bar. For more information about Android Studio full-screen immersive transparent status bar, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!