关键字:状态栏着色 透明状态栏 沉浸式 白底黑字
Github Demo:https://github.com/imflyn/Eyes
参考文章:
Android-transulcent-status-bar
Android 6.0状态栏使用灰色文字和图标
Android系统更改状态栏字体颜色
在谷歌官方的material设计文档中定义了新的状态栏设计。
https://material.io/guidelines/layout/structure.html#structure-system-bars
默认情况下,状态栏的颜色是黑色的。同时状态栏颜色也可以半透明或是指定任意一种颜色。
1.改变颜色后的状态栏
2.半透明状态栏
3.黑色状态栏
黑色icon或文字的状态栏
接下来讲一下具体实现
一.改变状态栏颜色
4.4-5.0的处理:
4.4-5.0还没有API可以直接修改状态栏颜色,所以必须先将状态栏设置为透明,然后在布局中添加一个背景为期望色值的View来作为状态栏的填充。
static void setStatusBarColor(Activity activity, int statusColor) { Window window = activity.getWindow();
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); } }
static void setContentTopPadding(Activity activity, int padding) { ViewGroup mContentView = (ViewGroup) activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT); mContentView.setPadding(0, padding, 0, 0); }
Android5.0以上的处理:
static void setStatusBarColor(Activity activity, int statusColor) {
Window window = activity.getWindow();
二.透明状态栏
4.4-5.0的处理:
static void translucentStatusBar(Activity activity) {
Window window = activity.getWindow();
5.0以上的处理:
static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
Window window = activity.getWindow();
三.使用CollapsingToolbarLayout使ToolBar具有折叠效果
类似图片中的效果
首先是XML布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="256dp" android:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="@color/colorPrimary" app:expandedTitleMarginEnd="64dp" app:expandedTitleMarginStart="48dp" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:statusBarScrim="@color/colorPrimary"> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:scaleType="centerCrop" android:src="@drawable/timg" app:layout_collapseMode="parallax" /> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingTop="24dp"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dip"> <TextView android:layout_width="match_parent" android:layout_height="240dip" android:text="A" android:textAppearance="@style/TextAppearance.AppCompat.Title" /> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="240dip" android:layout_margin="16dip"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="B" android:textAppearance="@style/TextAppearance.AppCompat.Title" /> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dip"> <TextView android:layout_width="match_parent" android:layout_height="240dip" android:text="C" android:textAppearance="@style/TextAppearance.AppCompat.Title" /> </android.support.v7.widget.CardView> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="240dip" android:layout_margin="16dip"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="D" android:textAppearance="@style/TextAppearance.AppCompat.Title" /> </android.support.v7.widget.CardView> </LinearLayout> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout>
4.4-5.0的处理:
static void setStatusBarColorForCollapsingToolbar(Activity activity, final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, int statusColor) { Window window = activity.getWindow();
5.0以上的处理:
static void setStatusBarColorForCollapsingToolbar(final Activity activity, final AppBarLayout appBarLayout, final CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, final int statusColor) { final Window window = activity.getWindow();
四.更改状态栏字体颜色
在Android 6.0的Api中提供了SYSTEM_UI_FLAG_LIGHT_STATUS_BAR这么一个常量,可以使状态栏文字设置为黑色,但对6.0以下是不起作用的。
小米和魅族的手机也可以达到这个效果,要做一些特殊的处理。可以参考小米和魅族的开发者文档。
Flyme沉浸式状态栏
MIUI 6 沉浸式状态栏调用方法
MIUI 9「状态栏黑色字符」实现方法变更通知
public static void setStatusBarLightMode(Activity activity, int color) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
小米MiUi修改状态栏方法
static boolean MIUISetStatusBarLightMode(Activity activity, boolean darkmode) { boolean result = false; Class<? extends Window> clazz = activity.getWindow().getClass(); try { int darkModeFlag = 0; Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); darkModeFlag = field.getInt(layoutParams); Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag); result = true; } catch (Exception e) { e.printStackTrace(); } return result; }
如果是MIUI9的系统还需要加上这段代码
Window window = 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);
Flyme修改状态栏方法
static boolean FlymeSetStatusBarLightMode(Activity activity, boolean darkmode) { boolean result = false; 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); result = true; } catch (Exception e) { e.printStackTrace(); } return result; }
结语:
更详细的参考Demo在github中,如果有错误也希望大家能够指出。