Simpler and more comprehensive material design status bar
Based on actual usage requirements, the following types of MD status bar are implemented in the simplest way. (Focus on the use of fitsSystemWindows)
0. Prerequisites
Theme. AppCompat. Light. DarkActionBar
TargetSdkVersion 23;
Support librariy 23.3.0
Styles-v19: True
Styles-v21 +: True @ Android: color/transparent
1. The most common type: Only one ToolBar
Layout:
. Java:
/***Simple Status Bar(ToolBar)**@ ParamActivity */Public static void setOrdinaryToolBar (Activity activity) {if (Build. VERSION.SDK_INT> = Build. VERSION_CODES.LOLLIPOP) {Activity. getWindow (). setStatusBarColor (ContextCompat. getColor (activity, R. color.ColorPrimaryDark);} Else if (Build. VERSION.SDK_INT= Build. VERSION_CODES.KITKAT) {SetKKStatusBar (activity, R. color.ColorPrimaryDark);}}
Effect:
Left: Android4.4 right: Android6.0
2. The full screen and transparent Status Bar (the picture is located under the status bar)
Layout:
windows="true" android:layout_height="wrap_content" />
. Java
/***Full Screen transparent Status Bar (the picture is located under the status bar) **@ ParamActivity */Public static void setImageTransparent (Activity activity) {if (Build. VERSION.SDK_INT> = Build. VERSION_CODES.LOLLIPOP) {Activity. getWindow (). getDecorView (). setSystemUiVisibility (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);}}
Effect:
Left: Android4.4 right: Android6.0
3. The full screen translucent status bar of the image (the image is located under the status bar)
Layout:
. Java:
/***Full Screen translucent status bar of the image (the image is located under the status bar) **@ ParamActivity */Public static void setImageTranslucent (Activity activity) {if (Build. VERSION.SDK_INT> = Build. VERSION_CODES.LOLLIPOP) {Activity. getWindow (). getDecorView (). setSystemUiVisibility (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); Activity. getWindow (). setStatusBarColor (ContextCompat. getColor (activity, R. color.StatusBar);} Else {setKKStatusBar (activity, R. color.StatusBar);}}
Effect:
Left: Android4.4 right: Android6.0
4. ToolBar + TabLayout Status Bar (the ToolBar is scalable)
Layout:
. Java:
/*** ToolBar + TabLayoutStatus Bar(ToolBarScalable)**@ ParamActivity */Public static void setToolbarTabLayout (Activity activity) {if (Build. VERSION.SDK_INT> = Build. VERSION_CODES.LOLLIPOP) {Activity. getWindow (). setStatusBarColor (ContextCompat. getColor (activity, R. color.ColorPrimaryDark));}}
Effect:
Android4.4:
Android6.0:
5. DrawerLayout + ToolBar + TabLayout Status Bar (ToolBar Scalable)
Layout:
. Java:
/*** DrawerLayout + ToolBar + TabLayoutStatus Bar(ToolBarScalable)**@ ParamActivity *@ ParamDrawerLayout *@ ParamCoordinatorLayout */Public static void setDrawerToolbarTabLayout (Activity activity, CoordinatorLayout coordinatorLayout) {if (Build. VERSION.SDK_INT= Build. VERSION_CODES.KITKAT) {ViewGroup contentLayout = (ViewGroup) activity. findViewById (android. R. id.Content); ContentLayout. getChildAt (0). setFitsSystemWindows (false); coordinatorLayout. setFitsSystemWindows (true); setkkkstatusbar (activity, R. color.StatusBar);}}
Effect:
Android4.4:
Android6.0:
6. CollapsingToolbarLayout Status Bar (foldable image)
Layout:
. Java:
/*** CollapsingToolbarLayoutStatus Bar(Foldable Image)**@ ParamActivity *@ ParamCoordinatorLayout *@ ParamAppBarLayout *@ ParamImageView *@ ParamToolbar */Public static void setCollapsingToolbar (Activity activity, CoordinatorLayout coordinatorLayout, AppBarLayout appBarLayout, ImageView imageView, Toolbar toolbar) {if (Build. VERSION.SDK_INT= Build. VERSION_CODES.KITKAT) {CoordinatorLayout. setFitsSystemWindows (false); appBarLayout. setFitsSystemWindows (false); imageView. setFitsSystemWindows (false); toolbar. setFitsSystemWindows (true); CollapsingToolbarLayout. layoutParams lp = (CollapsingToolbarLayout. layoutParams) toolbar. getLayoutParams (); lp. height = (int) (getStatusBarHeight (activity) + activity. getResources (). getDimension (R. dimen.Abc_action_bar_default_height_material); Toolbar. setLayoutParams (lp); setKKStatusBar (activity, R. color.StatusBar); SetCollapsingToolbarStatus (appBarLayout );}}
Effect:
Android4.4:
Android6.0:
7. DrawerLayout + ToolBar Status Bar
Layout:
. Java:
/*** DrawerLayout + ToolBarType status bar **@ ParamActivity */Public static void setDrawerToolbar (Activity activity) {if (Build. VERSION.SDK_INT= Build. VERSION_CODES.KITKAT) {ViewGroup contentLayout = (ViewGroup) activity. findViewById (android. R. id.Content); ContentLayout. getChildAt (0). setFitsSystemWindows (false); setkkkstatusbar (activity, R. color.StatusBar);}}
Others:
/*** Android4.4UpperCollapsingToolbarFoldingStatusBarShow and hide **@ ParamAppBarLayout */Private static void setCollapsingToolbarStatus (AppBarLayout appBarLayout) {ViewCompat. setAlpha (MStatusBarView, 1); appBarLayout. addOnOffsetChangedListener (new AppBarLayout. onOffsetChangedListener () {@ Override public void onOffsetChanged (AppBarLayout appBarLayout, int verticalOffset) {int maxScroll = appBarLayout. getTotalScrollRange (); float percentage = (float) Math. abs (verticalOffset)/(float) maxScroll; ViewCompat. setAlpha (MStatusBarView, Percentage) ;}}) ;}private static void setKKStatusBar (Activity activity, int statusBarColor) {ViewGroup contentView = (ViewGroup) activity. findViewById (android. R. id.Content);MStatusBarView= ContentView. getChildAt (0); // avoid repeated statusBarView if (MStatusBarView! = Null &&MStatusBarView. GetMeasuredHeight () = getStatusBarHeight (activity )){MStatusBarView. SetBackgroundColor (ContextCompat. getColor (activity, statusBarColor); return ;}MStatusBarView= New View (activity); ViewGroup. LayoutParams lp = new ViewGroup. LayoutParams (ViewGroup. LayoutParams.MATCH_PARENT, GetStatusBarHeight (activity ));MStatusBarView. SetBackgroundColor (ContextCompat. getColor (activity, statusBarColor); contentView. addView (MStatusBarView, Lp);} private static int getStatusBarHeight (Context context) {int resourceId = context. getResources (). getIdentifier ("status_bar_height", "dimen", "android"); return context. getResources (). getDimensionPixelSize (resourceId );}
Effect:
Android4.4:
Android6.0: