When it comes to the immersive experience of Android status bar, it has to be said that this is the pot left by the code designer to the developers. The version system api is fragmented and inconsistent. The subsequent versions are not compatible with the previous versions, so developers need to be compatible with different versions. Then a simple function has to consider all kinds of situations. A lot of code is purely for developers to find something to do. To be honest, these functions can be done in one sentence. Alas, don't say it. Post your own understanding, mainly for yourself. Others can also refer to it .
Besides, it's not necessary to have a deep understanding of these things. They don't have universality. It's best to use what others have summed up. Because, even if you understand that iOS, another development system, may not have these problems, the knowledge you have is useless.
References, mainly for reference https://www.jianshu.com/p/752f4551e134 On the basis of it, I'm talking about my own understanding: to realize the immersive status bar, there are only two ways: a. to set transparency to transfer the color of the content area to the status bar; b. to set the color of the status bar to the color of the content area;
Let's talk about it first. Official website How to discuss this point on the official website
- Dim system bar
// This example uses decor view, but you can use any visible view. View decorView = getActivity().getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE; decorView.setSystemUiVisibility(uiOptions)
- Hide status bar, navigation bar
- Turn on full screen mode
- Monitor interface visibility
These correspond to the above method a, through the setSystemUiVisibility and system ﹣ UI ﹣ flag settings. Now there is a problem that the official website is not consistent with most of the blogs. The official website only distinguishes between 4.0 and 4.1, not 4.4, 5.0 and 6.0. The two ideas are not the same, so this is a problem. I don't really understand why the ideas on the blog don't follow what is said on the official website (our application doesn't use actionBar basically, which is different from the mainstream implemented by google. The theme seems to be no aciton bar, no Title, and then define a titlebar, wild road play method, wild road solution In general, there are some connections between the two. These connections are methods or ideas. In fact, there is no need to make it clear. The reason is what I said above.
Well, according to some blogs on the Internet, they have a general solution: first, choose a theme, which is generally noActionBar's;
<!-- Base application theme. --> <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="android:windowAnimationStyle">@style/AnimationActivity</item> <item name="android:textAllCaps">false</item> <item name="android:windowActionBar">false</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFullscreen">true</item> </style>
Add a tool class:
package com.jaeger.library; import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Build; import android.support.annotation.ColorInt; import android.support.annotation.IntRange; import android.support.annotation.NonNull; import android.support.design.widget.CoordinatorLayout; import android.support.v4.widget.DrawerLayout; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.LinearLayout; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * Created by Jaeger on 16/2/14. * <p> * Email: chjie.jaeger@gmail.com * GitHub: https://github.com/laobie */ public class StatusBarUtil { public static final int DEFAULT_STATUS_BAR_ALPHA = 112; private static final int FAKE_STATUS_BAR_VIEW_ID = R.id.statusbarutil_fake_status_bar_view; private static final int FAKE_TRANSLUCENT_VIEW_ID = R.id.statusbarutil_translucent_view; private static final int TAG_KEY_HAVE_SET_OFFSET = -123; /** * Set status bar color * * @param activity activity to set * @param color Status bar color value */ public static void setColor(Activity activity, @ColorInt int color) { setColor(activity, color, DEFAULT_STATUS_BAR_ALPHA); } /** * Set status bar color * * @param activity activity to set * @param color Status bar color value * @param statusBarAlpha Status bar transparency */ public static void setColor(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha)); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { if (fakeStatusBarView.getVisibility() == View.GONE) { fakeStatusBarView.setVisibility(View.VISIBLE); } fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); } else { decorView.addView(createStatusBarView(activity, color, statusBarAlpha)); } setRootView(activity); } } /** * Set status bar color for slide back interface * * @param activity activity to set * @param color Status bar color value */ public static void setColorForSwipeBack(Activity activity, int color) { setColorForSwipeBack(activity, color, DEFAULT_STATUS_BAR_ALPHA); } /** * Set status bar color for slide back interface * * @param activity activity to set * @param color Status bar color value * @param statusBarAlpha Status bar transparency */ public static void setColorForSwipeBack(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { ViewGroup contentView = ((ViewGroup) activity.findViewById(android.R.id.content)); View rootView = contentView.getChildAt(0); int statusBarHeight = getStatusBarHeight(activity); if (rootView != null && rootView instanceof CoordinatorLayout) { final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) rootView; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { coordinatorLayout.setFitsSystemWindows(false); contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); boolean isNeedRequestLayout = contentView.getPaddingTop() < statusBarHeight; if (isNeedRequestLayout) { contentView.setPadding(0, statusBarHeight, 0, 0); coordinatorLayout.post(new Runnable() { @Override public void run() { coordinatorLayout.requestLayout(); } }); } } else { coordinatorLayout.setStatusBarBackgroundColor(calculateStatusColor(color, statusBarAlpha)); } } else { contentView.setPadding(0, statusBarHeight, 0, 0); contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); } setTransparentForWindow(activity); } } /** * Set the status bar solid without translucency * * @param activity activity to set * @param color Status bar color value */ public static void setColorNoTranslucent(Activity activity, @ColorInt int color) { setColor(activity, color, 0); } /** * Set status bar color (no translucency effect below 5.0, not recommended) * * @param activity activity to set * @param color Status bar color value */ @Deprecated public static void setColorDiff(Activity activity, @ColorInt int color) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } transparentStatusBar(activity); ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); // Remove translucent rectangle to avoid overlay View fakeStatusBarView = contentView.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { if (fakeStatusBarView.getVisibility() == View.GONE) { fakeStatusBarView.setVisibility(View.VISIBLE); } fakeStatusBarView.setBackgroundColor(color); } else { contentView.addView(createStatusBarView(activity, color)); } setRootView(activity); } /** * Make the status bar translucent * <p> * It is applicable to the interface with picture as background. At this time, the picture needs to be filled into the status bar * * @param activity activity to set */ public static void setTranslucent(Activity activity) { setTranslucent(activity, DEFAULT_STATUS_BAR_ALPHA); } /** * Make the status bar translucent * <p> * It is applicable to the interface with picture as background. At this time, the picture needs to be filled into the status bar * * @param activity activity to set * @param statusBarAlpha Status bar transparency */ public static void setTranslucent(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } setTransparent(activity); addTranslucentView(activity, statusBarAlpha); } /** * CoordinatorLayout for the root layout, making the status bar translucent * <p> * It is applicable to the interface with picture as background. At this time, the picture needs to be filled into the status bar * * @param activity activity to set * @param statusBarAlpha Status bar transparency */ public static void setTranslucentForCoordinatorLayout(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } transparentStatusBar(activity); addTranslucentView(activity, statusBarAlpha); } /** * Set status bar transparent * * @param activity activity to set */ public static void setTransparent(Activity activity) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } transparentStatusBar(activity); setRootView(activity); } /** * Make the status bar transparent (translucent effect above 5.0, not recommended) * <p> * It is applicable to the interface with picture as background. At this time, the picture needs to be filled into the status bar * * @param activity activity to set */ @Deprecated public static void setTranslucentDiff(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // Set status bar transparency activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); setRootView(activity); } } /** * Set status bar color change for DrawerLayout layout * * @param activity activity to set * @param drawerLayout DrawerLayout * @param color Status bar color value */ public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) { setColorForDrawerLayout(activity, drawerLayout, color, DEFAULT_STATUS_BAR_ALPHA); } /** * Set status bar color for DrawerLayout layout, solid * * @param activity activity to set * @param drawerLayout DrawerLayout * @param color Status bar color value */ public static void setColorNoTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) { setColorForDrawerLayout(activity, drawerLayout, color, 0); } /** * Set status bar color change for DrawerLayout layout * * @param activity activity to set * @param drawerLayout DrawerLayout * @param color Status bar color value * @param statusBarAlpha Status bar transparency */ public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().setStatusBarColor(Color.TRANSPARENT); } else { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } // Make a rectangle the size of the status bar // Add statusBarView to layout ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { if (fakeStatusBarView.getVisibility() == View.GONE) { fakeStatusBarView.setVisibility(View.VISIBLE); } fakeStatusBarView.setBackgroundColor(color); } else { contentLayout.addView(createStatusBarView(activity, color), 0); } // When the content layout is not LinearLayout, set padding top if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) { contentLayout.getChildAt(1) .setPadding(contentLayout.getPaddingLeft(), getStatusBarHeight(activity) + contentLayout.getPaddingTop(), contentLayout.getPaddingRight(), contentLayout.getPaddingBottom()); } // set a property setDrawerLayoutProperty(drawerLayout, contentLayout); addTranslucentView(activity, statusBarAlpha); } /** * Set DrawerLayout property * * @param drawerLayout DrawerLayout * @param drawerLayoutContentLayout DrawerLayout Content layout for */ private static void setDrawerLayoutProperty(DrawerLayout drawerLayout, ViewGroup drawerLayoutContentLayout) { ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1); drawerLayout.setFitsSystemWindows(false); drawerLayoutContentLayout.setFitsSystemWindows(false); drawerLayoutContentLayout.setClipToPadding(true); drawer.setFitsSystemWindows(false); } /** * Set the status bar color change for DrawerLayout layout (no translucency effect below 5.0, not recommended) * * @param activity activity to set * @param drawerLayout DrawerLayout * @param color Status bar color value */ @Deprecated public static void setColorForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // Make a rectangle the size of the status bar ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { if (fakeStatusBarView.getVisibility() == View.GONE) { fakeStatusBarView.setVisibility(View.VISIBLE); } fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, DEFAULT_STATUS_BAR_ALPHA)); } else { // Add statusBarView to layout contentLayout.addView(createStatusBarView(activity, color), 0); } // When the content layout is not LinearLayout, set padding top if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) { contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0); } // set a property setDrawerLayoutProperty(drawerLayout, contentLayout); } } /** * Set status bar transparency for DrawerLayout layout * * @param activity activity to set * @param drawerLayout DrawerLayout */ public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) { setTranslucentForDrawerLayout(activity, drawerLayout, DEFAULT_STATUS_BAR_ALPHA); } /** * Set status bar transparency for DrawerLayout layout * * @param activity activity to set * @param drawerLayout DrawerLayout */ public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @IntRange(from = 0, to = 255) int statusBarAlpha) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } setTransparentForDrawerLayout(activity, drawerLayout); addTranslucentView(activity, statusBarAlpha); } /** * Set status bar transparency for DrawerLayout layout * * @param activity activity to set * @param drawerLayout DrawerLayout */ public static void setTransparentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().setStatusBarColor(Color.TRANSPARENT); } else { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); // When the content layout is not LinearLayout, set padding top if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) { contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0); } // set a property setDrawerLayoutProperty(drawerLayout, contentLayout); } /** * Set the status bar transparency for the DrawerLayout layout (not recommended for translucency effects above 5.0) * * @param activity activity to set * @param drawerLayout DrawerLayout */ @Deprecated public static void setTranslucentForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // Set status bar transparency activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // Set content layout properties ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); contentLayout.setFitsSystemWindows(true); contentLayout.setClipToPadding(true); // Set drawer layout properties ViewGroup vg = (ViewGroup) drawerLayout.getChildAt(1); vg.setFitsSystemWindows(false); // Set DrawerLayout property drawerLayout.setFitsSystemWindows(false); } } /** * Set the status bar to be fully transparent for the interface with ImageView as the header * * @param activity activity to set * @param needOffsetView View to offset down */ public static void setTransparentForImageView(Activity activity, View needOffsetView) { setTranslucentForImageView(activity, 0, needOffsetView); } /** * Set the status bar transparency for the interface with ImageView as the header (use default transparency) * * @param activity activity to set * @param needOffsetView View to offset down */ public static void setTranslucentForImageView(Activity activity, View needOffsetView) { setTranslucentForImageView(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView); } /** * Set the status bar transparency for the interface with ImageView as the header * * @param activity activity to set * @param statusBarAlpha Status bar transparency * @param needOffsetView View to offset down */ public static void setTranslucentForImageView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha, View needOffsetView) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } setTransparentForWindow(activity); addTranslucentView(activity, statusBarAlpha); if (needOffsetView != null) { Object haveSetOffset = needOffsetView.getTag(TAG_KEY_HAVE_SET_OFFSET); if (haveSetOffset != null && (Boolean) haveSetOffset) { return; } ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) needOffsetView.getLayoutParams(); layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(activity), layoutParams.rightMargin, layoutParams.bottomMargin); needOffsetView.setTag(TAG_KEY_HAVE_SET_OFFSET, true); } } /** * Set the transparency of the status bar for the fragment header to be ImageView * * @param activity fragment Corresponding activity * @param needOffsetView View to offset down */ public static void setTranslucentForImageViewInFragment(Activity activity, View needOffsetView) { setTranslucentForImageViewInFragment(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView); } /** * Set the transparency of the status bar for the fragment header to be ImageView * * @param activity fragment Corresponding activity * @param needOffsetView View to offset down */ public static void setTransparentForImageViewInFragment(Activity activity, View needOffsetView) { setTranslucentForImageViewInFragment(activity, 0, needOffsetView); } /** * Set the transparency of the status bar for the fragment header to be ImageView * * @param activity fragment Corresponding activity * @param statusBarAlpha Status bar transparency * @param needOffsetView View to offset down */ public static void setTranslucentForImageViewInFragment(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha, View needOffsetView) { setTranslucentForImageView(activity, statusBarAlpha, needOffsetView); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { clearPreviousSetting(activity); } } /** * Hide pseudo status bar View * * @param activity Activity called */ public static void hideFakeStatusBarView(Activity activity) { ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { fakeStatusBarView.setVisibility(View.GONE); } View fakeTranslucentView = decorView.findViewById(FAKE_TRANSLUCENT_VIEW_ID); if (fakeTranslucentView != null) { fakeTranslucentView.setVisibility(View.GONE); } } @TargetApi(Build.VERSION_CODES.M) public static void setLightMode(Activity activity) { // setMIUIStatusBarDarkIcon(activity, true); // setMeizuStatusBarDarkIcon(activity, true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } } @TargetApi(Build.VERSION_CODES.M) public static void setDarkMode(Activity activity) { // setMIUIStatusBarDarkIcon(activity, false); // setMeizuStatusBarDarkIcon(activity, false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } } /** * Modify the status bar color above MIUI V6 */ private static void setMIUIStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) { Class<? extends Window> clazz = activity.getWindow().getClass(); try { Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); int darkModeFlag = field.getInt(layoutParams); Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); extraFlagField.invoke(activity.getWindow(), darkIcon ? darkModeFlag : 0, darkModeFlag); } catch (Exception e) { //e.printStackTrace(); } } /** * Modify Meizu status bar font color Flyme 4.0 */ private static void setMeizuStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) { try { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags"); darkFlag.setAccessible(true); meizuFlags.setAccessible(true); int bit = darkFlag.getInt(null); int value = meizuFlags.getInt(lp); if (darkIcon) { value |= bit; } else { value &= ~bit; } meizuFlags.setInt(lp, value); activity.getWindow().setAttributes(lp); } catch (Exception e) { //e.printStackTrace(); } } /////////////////////////////////////////////////////////////////////////////////// @TargetApi(Build.VERSION_CODES.KITKAT) private static void clearPreviousSetting(Activity activity) { ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { decorView.removeView(fakeStatusBarView); ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0); rootView.setPadding(0, 0, 0, 0); } } /** * Add translucent rectangle bar * * @param activity activity to set * @param statusBarAlpha Transparent value */ private static void addTranslucentView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) { ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); View fakeTranslucentView = contentView.findViewById(FAKE_TRANSLUCENT_VIEW_ID); if (fakeTranslucentView != null) { if (fakeTranslucentView.getVisibility() == View.GONE) { fakeTranslucentView.setVisibility(View.VISIBLE); } fakeTranslucentView.setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0)); } else { contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha)); } } /** * Generate a colored rectangle of the same size as the status bar * * @param activity activity to set * @param color Status bar color value * @return Status bar rectangle */ private static View createStatusBarView(Activity activity, @ColorInt int color) { return createStatusBarView(activity, color, 0); } /** * Generates a translucent rectangle of the same size as the status bar * * @param activity activity to set * @param color Status bar color value * @param alpha Transparent value * @return Status bar rectangle */ private static View createStatusBarView(Activity activity, @ColorInt int color, int alpha) { // Draw a rectangle as high as the status bar View statusBarView = new View(activity); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); statusBarView.setLayoutParams(params); statusBarView.setBackgroundColor(calculateStatusColor(color, alpha)); statusBarView.setId(FAKE_STATUS_BAR_VIEW_ID); return statusBarView; } /** * Set root layout parameters */ private static void setRootView(Activity activity) { ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content); for (int i = 0, count = parent.getChildCount(); i < count; i++) { View childView = parent.getChildAt(i); if (childView instanceof ViewGroup) { childView.setFitsSystemWindows(true); ((ViewGroup) childView).setClipToPadding(true); } } } /** * Set transparency */ private static void setTransparentForWindow(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().setStatusBarColor(Color.TRANSPARENT); activity.getWindow() .getDecorView() .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity.getWindow() .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } } /** * Make the status bar transparent */ @TargetApi(Build.VERSION_CODES.KITKAT) private static void transparentStatusBar(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); activity.getWindow().setStatusBarColor(Color.TRANSPARENT); } else { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } } /** * Create a translucent rectangular View * * @param alpha Transparent value * @return Translucent View */ private static View createTranslucentStatusBarView(Activity activity, int alpha) { // Draw a rectangle as high as the status bar View statusBarView = new View(activity); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); statusBarView.setLayoutParams(params); statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0)); statusBarView.setId(FAKE_TRANSLUCENT_VIEW_ID); return statusBarView; } /** * Get status bar height * * @param context context * @return Status bar height */ private static int getStatusBarHeight(Context context) { // Get status bar height int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); return context.getResources().getDimensionPixelSize(resourceId); } /** * Calculate status bar color * * @param color color value * @param alpha alpha value * @return Final status bar color */ private static int calculateStatusColor(@ColorInt int color, int alpha) { if (alpha == 0) { return color; } float a = 1 - alpha / 255f; int red = color >> 16 & 0xff; int green = color >> 8 & 0xff; int blue = color & 0xff; red = (int) (red * a + 0.5); green = (int) (green * a + 0.5); blue = (int) (blue * a + 0.5); return 0xff << 24 | red << 16 | green << 8 | blue; } }
For example, we can use onCreate in baseability
setTranslucent(this);
setLightMode(this);
The first method is to set the immersion mode. The second method is to solve the problem that when the theme color and the status bar color are the same (such as white), the status bar cannot be seen. If it is greater than 4.4, set the flag ﹣ transmission ﹣ status; if it is greater than 21, it can be realized through setstatus barcolor;
The second method is to set the color, that is, call setColor (this, "color") in baseActivity. If an interface wants different colors, you can use setColor to override the settings in baseActivity interface. The essence is the same as the first one.
Finally, in combination with my previous recommendation, it's almost enough to have a good understanding.