Android 手機狀態列變透明以及更換顏色
阿新 • • 發佈:2019-01-29
我直接封裝好了一個工具類,可以直接透明,或者自定義顏色,直
接呼叫就可以,具體的呼叫方法解除安裝後面,共需要三個工具類我全部貼在下面
一.工具類1( EyesKitKat )
@TargetApi(Build.VERSION_CODES.KITKAT) class EyesKitKat { private static final String TAG_FAKE_STATUS_BAR_VIEW = "statusBarView"; private static final String TAG_MARGIN_ADDED = "marginAdded"; static void setStatusBarColor(Activity activity, int statusColor) { Window window = activity.getWindow(); //設定Window為全透明 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT); //獲取父佈局 View mContentChild = mContentView.getChildAt(0); //獲取狀態列高度 int statusBarHeight = getStatusBarHeight(activity); //如果已經存在假狀態列則移除,防止重複新增 removeFakeStatusBarViewIfExist(activity); //新增一個View來作為狀態列的填充 addFakeStatusBarView(activity, statusColor, statusBarHeight); //設定子控制元件到狀態列的間距 addMarginTopToContentChild(mContentChild, statusBarHeight); //不預留系統欄位置 if (mContentChild != null) { mContentChild.setFitsSystemWindows(false); } //如果在Activity中使用了ActionBar則需要再將佈局與狀態列的高度跳高一個ActionBar的高度,否則內容會被ActionBar遮擋 int action_bar_id = activity.getResources().getIdentifier("action_bar", "id", activity.getPackageName()); View view = activity.findViewById(action_bar_id); if (view != null) { TypedValue typedValue = new TypedValue(); if (activity.getTheme().resolveAttribute(R.attr.actionBarSize, typedValue, true)) { int actionBarHeight = TypedValue.complexToDimensionPixelSize(typedValue.data, activity.getResources().getDisplayMetrics()); Eyes.setContentTopPadding(activity, actionBarHeight); } } } static void translucentStatusBar(Activity activity) { Window window = activity.getWindow(); //設定Window為透明 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); ViewGroup mContentView = activity.findViewById(Window.ID_ANDROID_CONTENT); View mContentChild = mContentView.getChildAt(0); //移除已經存在假狀態列則,並且取消它的Margin間距 removeFakeStatusBarViewIfExist(activity); removeMarginTopOfContentChild(mContentChild, getStatusBarHeight(activity)); if (mContentChild != null) { //fitsSystemWindow 為 false, 不預留系統欄位置. mContentChild.setFitsSystemWindows(false); } } static void setStatusBarColorForCollapsingToolbar(final Activity activity, final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, int statusColor) { Window window = activity.getWindow(); //設定Window為全透明 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT); //AppBarLayout,CollapsingToolbarLayout,ToolBar,ImageView的fitsSystemWindow統一改為false, 不預留系統欄位置. View mContentChild = mContentView.getChildAt(0); mContentChild.setFitsSystemWindows(false); ((View) appBarLayout.getParent()).setFitsSystemWindows(false); appBarLayout.setFitsSystemWindows(false); collapsingToolbarLayout.setFitsSystemWindows(false); collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(false); toolbar.setFitsSystemWindows(false); //為Toolbar新增一個狀態列的高度, 同時為Toolbar新增paddingTop,使Toolbar覆蓋狀態列,ToolBar的title可以正常顯示. if (toolbar.getTag() == null) { CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams(); int statusBarHeight = getStatusBarHeight(activity); lp.height += statusBarHeight; toolbar.setLayoutParams(lp); toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusBarHeight, toolbar.getPaddingRight(), toolbar.getPaddingBottom()); toolbar.setTag(true); } //移除已經存在假狀態列則,並且取消它的Margin間距 int statusBarHeight = getStatusBarHeight(activity); removeFakeStatusBarViewIfExist(activity); removeMarginTopOfContentChild(mContentChild, statusBarHeight); //新增一個View來作為狀態列的填充 final View statusView = addFakeStatusBarView(activity, statusColor, statusBarHeight); CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior(); if (behavior != null && behavior instanceof AppBarLayout.Behavior) { int verticalOffset = ((AppBarLayout.Behavior) behavior).getTopAndBottomOffset(); if (Math.abs(verticalOffset) > appBarLayout.getHeight() - collapsingToolbarLayout.getScrimVisibleHeightTrigger()) { statusView.setAlpha(1f); } else { statusView.setAlpha(0f); } } else { statusView.setAlpha(0f); } appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if (Math.abs(verticalOffset) > appBarLayout.getHeight() - collapsingToolbarLayout.getScrimVisibleHeightTrigger()) { //toolbar被摺疊時顯示狀態列 if (statusView.getAlpha() == 0) { statusView.animate().cancel(); statusView.animate().alpha(1f).setDuration(collapsingToolbarLayout.getScrimAnimationDuration()).start(); } } else { //toolbar展開時顯示狀態列 if (statusView.getAlpha() == 1) { statusView.animate().cancel(); statusView.animate().alpha(0f).setDuration(collapsingToolbarLayout.getScrimAnimationDuration()).start(); } } } }); } static void setStatusBarWhiteForCollapsingToolbar(final Activity activity, AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, final int statusBarColor) { final Window window = activity.getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT); View mContentChild = mContentView.getChildAt(0); mContentChild.setFitsSystemWindows(false); ((View) appBarLayout.getParent()).setFitsSystemWindows(false); appBarLayout.setFitsSystemWindows(false); collapsingToolbarLayout.setFitsSystemWindows(false); collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(false); toolbar.setFitsSystemWindows(false); if (toolbar.getTag() == null) { CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams(); int statusBarHeight = getStatusBarHeight(activity); lp.height += statusBarHeight; toolbar.setLayoutParams(lp); toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusBarHeight, toolbar.getPaddingRight(), toolbar.getPaddingBottom()); toolbar.setTag(true); } int statusBarHeight = getStatusBarHeight(activity); int color = Color.BLACK; try { Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); color = statusBarColor; } catch (ClassNotFoundException e) { e.printStackTrace(); } try { Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); color = statusBarColor; } catch (NoSuchFieldException e) { e.printStackTrace(); } final View statusView = addFakeStatusBarView(activity, color, statusBarHeight); CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior(); if (behavior != null && behavior instanceof AppBarLayout.Behavior) { int verticalOffset = ((AppBarLayout.Behavior) behavior).getTopAndBottomOffset(); if (Math.abs(verticalOffset) > appBarLayout.getHeight() - collapsingToolbarLayout.getScrimVisibleHeightTrigger()) { statusView.setAlpha(1f); } else { statusView.setAlpha(0f); } } else { statusView.setAlpha(0f); } appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { private final static int EXPANDED = 0; private final static int COLLAPSED = 1; private int appBarLayoutState; @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if (Math.abs(verticalOffset) >= (appBarLayout.getTotalScrollRange() - Eyes.getPxFromDp(activity, 56))) { if (appBarLayoutState != COLLAPSED) { appBarLayoutState = COLLAPSED; if (Eyes.MIUISetStatusBarLightMode(activity, true) || Eyes.FlymeSetStatusBarLightMode(activity, true)) { } if (statusView.getAlpha() == 0) { statusView.animate().cancel(); statusView.animate().alpha(1f).setDuration(collapsingToolbarLayout.getScrimAnimationDuration()).start(); } } } else { if (appBarLayoutState != EXPANDED) { appBarLayoutState = EXPANDED; if (Eyes.MIUISetStatusBarLightMode(activity, false) || Eyes.FlymeSetStatusBarLightMode(activity, false)) { } if (statusView.getAlpha() == 1) { statusView.animate().cancel(); statusView.animate().alpha(0f).setDuration(collapsingToolbarLayout.getScrimAnimationDuration()).start(); } translucentStatusBar(activity); } } } }); } private static void removeFakeStatusBarViewIfExist(Activity activity) { Window window = activity.getWindow(); ViewGroup mDecorView = (ViewGroup) window.getDecorView(); View fakeView = mDecorView.findViewWithTag(TAG_FAKE_STATUS_BAR_VIEW); if (fakeView != null) { mDecorView.removeView(fakeView); } } private static View addFakeStatusBarView(Activity activity, int statusBarColor, int statusBarHeight) { Window window = activity.getWindow(); ViewGroup mDecorView = (ViewGroup) window.getDecorView(); View mStatusBarView = new View(activity); FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight); layoutParams.gravity = Gravity.TOP; mStatusBarView.setLayoutParams(layoutParams); mStatusBarView.setBackgroundColor(statusBarColor); mStatusBarView.setTag(TAG_FAKE_STATUS_BAR_VIEW); mDecorView.addView(mStatusBarView); return mStatusBarView; } private static void addMarginTopToContentChild(View mContentChild, int statusBarHeight) { if (mContentChild == null) { return; } if (!TAG_MARGIN_ADDED.equals(mContentChild.getTag())) { FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mContentChild.getLayoutParams(); lp.topMargin += statusBarHeight; mContentChild.setLayoutParams(lp); mContentChild.setTag(TAG_MARGIN_ADDED); } } private static int getStatusBarHeight(Context context) { int result = 0; int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resId > 0) { result = context.getResources().getDimensionPixelOffset(resId); } return result; } private static void removeMarginTopOfContentChild(View mContentChild, int statusBarHeight) { if (mContentChild == null) { return; } if (TAG_MARGIN_ADDED.equals(mContentChild.getTag())) { FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mContentChild.getLayoutParams(); lp.topMargin -= statusBarHeight; mContentChild.setLayoutParams(lp); mContentChild.setTag(null); } } }
二.工具類2( EyesLollipop )
@TargetApi(Build.VERSION_CODES.LOLLIPOP) class EyesLollipop { static void setStatusBarColor(Activity activity, int statusColor) { Window window = activity.getWindow(); //取消狀態列透明 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //新增Flag把狀態列設為可繪製模式 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); //設定狀態列顏色 window.setStatusBarColor(statusColor); //設定系統狀態列處於可見狀態 window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); //讓view不根據系統視窗來調整自己的佈局 ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT); View mChildView = mContentView.getChildAt(0); if (mChildView != null) { mChildView.setFitsSystemWindows(false); ViewCompat.requestApplyInsets(mChildView); } } static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) { Window window = activity.getWindow(); //新增Flag把狀態列設為可繪製模式 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); if (hideStatusBarBackground) { //如果為全透明模式,取消設定Window半透明的Flag window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //設定狀態列為透明 window.setStatusBarColor(Color.TRANSPARENT); //設定window的狀態列不可見 window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } else { //如果為半透明模式,新增設定Window半透明的Flag window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //設定系統狀態列處於可見狀態 window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); } //view不根據系統視窗來調整自己的佈局 ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT); View mChildView = mContentView.getChildAt(0); if (mChildView != null) { mChildView.setFitsSystemWindows(false); ViewCompat.requestApplyInsets(mChildView); } } static void setStatusBarColorForCollapsingToolbar(final Activity activity, final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, final int statusColor) { final Window window = activity.getWindow(); //取消設定Window半透明的Flag window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); ////新增Flag把狀態列設為可繪製模式 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); //設定狀態列為透明 window.setStatusBarColor(Color.TRANSPARENT); //設定系統狀態列處於可見狀態 window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); //通過OnApplyWindowInsetsListener()使Layout在繪製過程中將View向下偏移了,使collapsingToolbarLayout可以佔據狀態列 ViewCompat.setOnApplyWindowInsetsListener(collapsingToolbarLayout, new OnApplyWindowInsetsListener() { @Override public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { return insets; } }); ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT); View mChildView = mContentView.getChildAt(0); //view不根據系統視窗來調整自己的佈局 if (mChildView != null) { mChildView.setFitsSystemWindows(false); ViewCompat.requestApplyInsets(mChildView); } ((View) appBarLayout.getParent()).setFitsSystemWindows(false); appBarLayout.setFitsSystemWindows(false); collapsingToolbarLayout.setFitsSystemWindows(false); collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(false); //設定狀態列的顏色 collapsingToolbarLayout.setStatusBarScrimColor(statusColor); toolbar.setFitsSystemWindows(false); //為Toolbar新增一個狀態列的高度, 同時為Toolbar新增paddingTop,使Toolbar覆蓋狀態列,ToolBar的title可以正常顯示. if (toolbar.getTag() == null) { CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams(); int statusBarHeight = getStatusBarHeight(activity); lp.height += statusBarHeight; toolbar.setLayoutParams(lp); toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusBarHeight, toolbar.getPaddingRight(), toolbar.getPaddingBottom()); toolbar.setTag(true); } appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { private final static int EXPANDED = 0; private final static int COLLAPSED = 1; private int appBarLayoutState; @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { //toolbar被摺疊時顯示狀態列 if (Math.abs(verticalOffset) > collapsingToolbarLayout.getScrimVisibleHeightTrigger()) { if (appBarLayoutState != COLLAPSED) { appBarLayoutState = COLLAPSED;//修改狀態標記為摺疊 setStatusBarColor(activity, statusColor); } } else { //toolbar顯示時同時顯示狀態列 if (appBarLayoutState != EXPANDED) { appBarLayoutState = EXPANDED;//修改狀態標記為展開 translucentStatusBar(activity, true); } } } }); } static void setStatusBarWhiteForCollapsingToolbar(final Activity activity, final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout, final Toolbar toolbar, final int statusBarColor) { final Window window = activity.getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.TRANSPARENT); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); ViewCompat.setOnApplyWindowInsetsListener(collapsingToolbarLayout, new OnApplyWindowInsetsListener() { @Override public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { return insets; } }); ViewGroup mContentView = window.findViewById(Window.ID_ANDROID_CONTENT); View mChildView = mContentView.getChildAt(0); if (mChildView != null) { mChildView.setFitsSystemWindows(false); ViewCompat.requestApplyInsets(mChildView); } ((View) appBarLayout.getParent()).setFitsSystemWindows(false); appBarLayout.setFitsSystemWindows(false); toolbar.setFitsSystemWindows(false); if (toolbar.getTag() == null) { CollapsingToolbarLayout.LayoutParams lp = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams(); int statusBarHeight = getStatusBarHeight(activity); lp.height += statusBarHeight; toolbar.setLayoutParams(lp); toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusBarHeight, toolbar.getPaddingRight(), toolbar.getPaddingBottom()); toolbar.setTag(true); } collapsingToolbarLayout.setFitsSystemWindows(false); collapsingToolbarLayout.getChildAt(0).setFitsSystemWindows(false); collapsingToolbarLayout.setStatusBarScrimColor(statusBarColor); appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { private final static int EXPANDED = 0; private final static int COLLAPSED = 1; private int appBarLayoutState; @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { //toolbar被摺疊時顯示狀態列 if (Math.abs(verticalOffset) > collapsingToolbarLayout.getScrimVisibleHeightTrigger()) { if (appBarLayoutState != COLLAPSED) { appBarLayoutState = COLLAPSED;//修改狀態標記為摺疊 //先判斷是否為小米裝置,設定狀態列不成功判斷是否為6.0以上裝置,不是6.0以上裝置再判斷是否為魅族裝置,不是魅族裝置就只設置狀態列顏色 if (Eyes.MIUISetStatusBarLightMode(activity, true)) { return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { activity.getWindow().setBackgroundDrawableResource(android.R.color.transparent); activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); activity.getWindow().setStatusBarColor(statusBarColor); } else if (!Eyes.FlymeSetStatusBarLightMode(activity, true)) { setStatusBarColor(activity, statusBarColor); } } } else { //toolbar顯示時同時顯示狀態列 if (appBarLayoutState != EXPANDED) { appBarLayoutState = EXPANDED;//修改狀態標記為展開 if (Eyes.MIUISetStatusBarLightMode(activity, false)) { translucentStatusBar(activity, true); return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE); } else if (Eyes.FlymeSetStatusBarLightMode(activity, true)) { } translucentStatusBar(activity, true); } } } }); } private static int getStatusBarHeight(Context context) { int result = 0; int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resId > 0) { result = context.getResources().getDimensionPixelOffset(resId); } return result; } }
三,工具類3,真正實現類( Eyes )
public class Eyes { public static void setStatusBarColor(Activity activity, int statusColor) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { EyesLollipop.setStatusBarColor(activity, statusColor); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { EyesKitKat.setStatusBarColor(activity, statusColor); } } public static void translucentStatusBar(Activity activity) { translucentStatusBar(activity, false); } public static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { EyesLollipop.translucentStatusBar(activity, hideStatusBarBackground); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { EyesKitKat.translucentStatusBar(activity); } } public static void setStatusBarColorForCollapsingToolbar(@NonNull Activity activity, AppBarLayout appBarLayout, CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, @ColorInt int statusColor) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { EyesLollipop.setStatusBarColorForCollapsingToolbar(activity, appBarLayout, collapsingToolbarLayout, toolbar, statusColor); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { EyesKitKat.setStatusBarColorForCollapsingToolbar(activity, appBarLayout, collapsingToolbarLayout, toolbar, statusColor); } } public static void setStatusBarLightMode(Activity activity, int color) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //判斷是否為小米或魅族手機,如果是則將狀態列文字改為黑色 if (MIUISetStatusBarLightMode(activity, true) || FlymeSetStatusBarLightMode(activity, true)) { //設定狀態列為指定顏色 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 activity.getWindow().setStatusBarColor(color); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4 //呼叫修改狀態列顏色的方法 setStatusBarColor(activity, color); } } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //如果是6.0以上將狀態列文字改為黑色,並設定狀態列顏色 // activity.getWindow().setBackgroundDrawableResource(android.R.color.transparent); activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); activity.getWindow().setStatusBarColor(color); // fitsSystemWindow 為 false, 不預留系統欄位置. ViewGroup mContentView = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT); View mChildView = mContentView.getChildAt(0); if (mChildView != null) { mChildView.setFitsSystemWindows(true); ViewCompat.requestApplyInsets(mChildView); } } else { // 針對本應用,低版本手機狀態列設為黑色 setStatusBarColor(activity, Color.BLACK); } } } public static void setStatusBarLightForCollapsingToolbar(Activity activity, AppBarLayout appBarLayout, CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, int statusBarColor) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { EyesLollipop.setStatusBarWhiteForCollapsingToolbar(activity, appBarLayout, collapsingToolbarLayout, toolbar, statusBarColor); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { EyesKitKat.setStatusBarWhiteForCollapsingToolbar(activity, appBarLayout, collapsingToolbarLayout, toolbar, statusBarColor); } } /** * MIUI的沉浸支援透明白色字型和透明黑色字型 * https://dev.mi.com/console/doc/detail?pId=1159 */ static boolean MIUISetStatusBarLightMode(Activity activity, boolean darkmode) { try { Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); Window window = activity.getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); Class<? extends Window> clazz = activity.getWindow().getClass(); 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(), darkmode ? darkModeFlag : 0, darkModeFlag); return true; } catch (Exception e) { // e.printStackTrace(); } return false; } /** * 設定狀態列圖示為深色和魅族特定的文字風格,Flyme4.0以上 */ static boolean FlymeSetStatusBarLightMode(Activity activity, boolean darkmode) { 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 (darkmode) { value |= bit; } else { value &= ~bit; } meizuFlags.setInt(lp, value); activity.getWindow().setAttributes(lp); return true; } catch (Exception e) { // e.printStackTrace(); } return false; } static void setContentTopPadding(Activity activity, int padding) { ViewGroup mContentView = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT); mContentView.setPadding(0, padding, 0, 0); } static int getPxFromDp(Context context, float dp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); } }
四.呼叫方法
//自定義顏色,第二個引數直接寫顏色就可以
Eyes.setStatusBarLightMode(MainActivity.this, Color.WHITE);
//透明色,第二個引數直接寫true就可以
Eyes.translucentStatusBar(MainActivity.this, true);
好了,有什麼不懂的可以問我!