實(shí)現(xiàn)步驟丧枪,只需4步:
1.新建工具類(lèi)StatusBarUtil:
public class StatusBarUtil {
public static final int DEFAULT_STATUS_BAR_ALPHA =112;
? ? private static final int FAKE_STATUS_BAR_VIEW_ID = com.jaeger.library.R.id.statusbarutil_fake_status_bar_view;
? ? private static final int FAKE_TRANSLUCENT_VIEW_ID = com.jaeger.library.R.id.statusbarutil_translucent_view;
? ? private static final int TAG_KEY_HAVE_SET_OFFSET = -123;
? ? /**
* 設(shè)置狀態(tài)欄顏色
*
? ? * @param activity 需要設(shè)置的 activity
? ? * @param color? ? 狀態(tài)欄顏色值
*/
? ? public static void setColor(Activity activity, @ColorInt int color) {
setColor(activity, color, DEFAULT_STATUS_BAR_ALPHA);
? ? }
/**
* 設(shè)置狀態(tài)欄顏色
*
? ? * @param activity? ? ? 需要設(shè)置的activity
? ? * @param color? ? ? ? ? 狀態(tài)欄顏色值
? ? * @param statusBarAlpha 狀態(tài)欄透明度
*/
? ? public static void setColor(Activity activity, @ColorInt int color, 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);
? ? ? ? }
}
/**
* 為滑動(dòng)返回界面設(shè)置狀態(tài)欄顏色
*
? ? * @param activity 需要設(shè)置的activity
? ? * @param color? ? 狀態(tài)欄顏色值
*/
? ? public static void setColorForSwipeBack(Activity activity, int color) {
setColorForSwipeBack(activity, color, DEFAULT_STATUS_BAR_ALPHA);
? ? }
/**
* 為滑動(dòng)返回界面設(shè)置狀態(tài)欄顏色
*
? ? * @param activity? ? ? 需要設(shè)置的activity
? ? * @param color? ? ? ? ? 狀態(tài)欄顏色值
? ? * @param statusBarAlpha 狀態(tài)欄透明度
*/
? ? public static void setColorForSwipeBack(Activity activity, @ColorInt int color, 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 && rootViewinstanceof 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);
? ? ? ? }
}
/**
* 設(shè)置狀態(tài)欄純色 不加半透明效果
*
? ? * @param activity 需要設(shè)置的 activity
? ? * @param color? ? 狀態(tài)欄顏色值
*/
? ? public static void setColorNoTranslucent(Activity activity, @ColorInt int color) {
setColor(activity, color, 0);
? ? }
/**
* 設(shè)置狀態(tài)欄顏色(5.0以下無(wú)半透明效果,不建議使用)
*
? ? * @param activity 需要設(shè)置的 activity
? ? * @param color? ? 狀態(tài)欄顏色值
*/
? ? @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);
? ? ? ? // 移除半透明矩形,以免疊加
? ? ? ? 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);
? ? }
/**
* 使?fàn)顟B(tài)欄半透明
? ? *
? ? * 適用于圖片作為背景的界面,此時(shí)需要圖片填充到狀態(tài)欄
*
? ? * @param activity 需要設(shè)置的activity
*/
? ? public static void setTranslucent(Activity activity) {
setTranslucent(activity, DEFAULT_STATUS_BAR_ALPHA);
? ? }
/**
* 使?fàn)顟B(tài)欄半透明
? ? *
? ? * 適用于圖片作為背景的界面,此時(shí)需要圖片填充到狀態(tài)欄
*
? ? * @param activity? ? ? 需要設(shè)置的activity
? ? * @param statusBarAlpha 狀態(tài)欄透明度
*/
? ? public static void setTranslucent(Activity activity, int statusBarAlpha) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
? ? ? ? }
setTransparent(activity);
? ? ? ? addTranslucentView(activity, statusBarAlpha);
? ? }
/**
* 針對(duì)根布局是 CoordinatorLayout, 使?fàn)顟B(tài)欄半透明
? ? *
? ? * 適用于圖片作為背景的界面,此時(shí)需要圖片填充到狀態(tài)欄
*
? ? * @param activity? ? ? 需要設(shè)置的activity
? ? * @param statusBarAlpha 狀態(tài)欄透明度
*/
? ? public static void setTranslucentForCoordinatorLayout(Activity activity, int statusBarAlpha) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
? ? ? ? }
transparentStatusBar(activity);
? ? ? ? addTranslucentView(activity, statusBarAlpha);
? ? }
/**
* 設(shè)置狀態(tài)欄全透明
*
? ? * @param activity 需要設(shè)置的activity
*/
? ? public static void setTransparent(Activity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
? ? ? ? }
transparentStatusBar(activity);
? ? ? ? setRootView(activity);
? ? }
/**
* 使?fàn)顟B(tài)欄透明(5.0以上半透明效果,不建議使用)
? ? *
? ? * 適用于圖片作為背景的界面,此時(shí)需要圖片填充到狀態(tài)欄
*
? ? * @param activity 需要設(shè)置的activity
*/
? ? @Deprecated
? ? public static void setTranslucentDiff(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 設(shè)置狀態(tài)欄透明
? ? ? ? ? ? activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
? ? ? ? ? ? setRootView(activity);
? ? ? ? }
}
/**
* 為DrawerLayout 布局設(shè)置狀態(tài)欄變色
*
? ? * @param activity? ? 需要設(shè)置的activity
? ? * @param drawerLayout DrawerLayout
? ? * @param color? ? ? ? 狀態(tài)欄顏色值
*/
? ? public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
setColorForDrawerLayout(activity, drawerLayout, color, DEFAULT_STATUS_BAR_ALPHA);
? ? }
/**
* 為DrawerLayout 布局設(shè)置狀態(tài)欄顏色,純色
*
? ? * @param activity? ? 需要設(shè)置的activity
? ? * @param drawerLayout DrawerLayout
? ? * @param color? ? ? ? 狀態(tài)欄顏色值
*/
? ? public static void setColorNoTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) {
setColorForDrawerLayout(activity, drawerLayout, color, 0);
? ? }
/**
* 為DrawerLayout 布局設(shè)置狀態(tài)欄變色
*
? ? * @param activity? ? ? 需要設(shè)置的activity
? ? * @param drawerLayout? DrawerLayout
? ? * @param color? ? ? ? ? 狀態(tài)欄顏色值
? ? * @param statusBarAlpha 狀態(tài)欄透明度
*/
? ? public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color,
? ? ? ? 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);
? ? ? ? }
// 生成一個(gè)狀態(tài)欄大小的矩形
// 添加 statusBarView 到布局中
? ? ? ? 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);
? ? ? ? }
// 內(nèi)容布局不是 LinearLayout 時(shí),設(shè)置padding top
? ? ? ? if (!(contentLayoutinstanceof LinearLayout) && contentLayout.getChildAt(1) !=null) {
contentLayout.getChildAt(1)
.setPadding(contentLayout.getPaddingLeft(), getStatusBarHeight(activity) + contentLayout.getPaddingTop(),
? ? ? ? ? ? ? ? ? ? contentLayout.getPaddingRight(), contentLayout.getPaddingBottom());
? ? ? ? }
// 設(shè)置屬性
? ? ? ? setDrawerLayoutProperty(drawerLayout, contentLayout);
? ? ? ? addTranslucentView(activity, statusBarAlpha);
? ? }
/**
* 設(shè)置 DrawerLayout 屬性
*
? ? * @param drawerLayout? ? ? ? ? ? ? DrawerLayout
? ? * @param drawerLayoutContentLayout DrawerLayout 的內(nèi)容布局
*/
? ? 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);
? ? }
/**
* 為DrawerLayout 布局設(shè)置狀態(tài)欄變色(5.0以下無(wú)半透明效果,不建議使用)
*
? ? * @param activity? ? 需要設(shè)置的activity
? ? * @param drawerLayout DrawerLayout
? ? * @param color? ? ? ? 狀態(tài)欄顏色值
*/
? ? @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);
? ? ? ? ? ? // 生成一個(gè)狀態(tài)欄大小的矩形
? ? ? ? ? ? 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 {
// 添加 statusBarView 到布局中
? ? ? ? ? ? ? ? contentLayout.addView(createStatusBarView(activity, color), 0);
? ? ? ? ? ? }
// 內(nèi)容布局不是 LinearLayout 時(shí),設(shè)置padding top
? ? ? ? ? ? if (!(contentLayoutinstanceof LinearLayout) && contentLayout.getChildAt(1) !=null) {
contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
? ? ? ? ? ? }
// 設(shè)置屬性
? ? ? ? ? ? setDrawerLayoutProperty(drawerLayout, contentLayout);
? ? ? ? }
}
/**
* 為 DrawerLayout 布局設(shè)置狀態(tài)欄透明
*
? ? * @param activity? ? 需要設(shè)置的activity
? ? * @param drawerLayout DrawerLayout
*/
? ? public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
setTranslucentForDrawerLayout(activity, drawerLayout, DEFAULT_STATUS_BAR_ALPHA);
? ? }
/**
* 為 DrawerLayout 布局設(shè)置狀態(tài)欄透明
*
? ? * @param activity? ? 需要設(shè)置的activity
? ? * @param drawerLayout DrawerLayout
*/
? ? public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout, int statusBarAlpha) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
? ? ? ? }
setTransparentForDrawerLayout(activity, drawerLayout);
? ? ? ? addTranslucentView(activity, statusBarAlpha);
? ? }
/**
* 為 DrawerLayout 布局設(shè)置狀態(tài)欄透明
*
? ? * @param activity? ? 需要設(shè)置的activity
? ? * @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);
? ? ? ? // 內(nèi)容布局不是 LinearLayout 時(shí),設(shè)置padding top
? ? ? ? if (!(contentLayoutinstanceof LinearLayout) && contentLayout.getChildAt(1) !=null) {
contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
? ? ? ? }
// 設(shè)置屬性
? ? ? ? setDrawerLayoutProperty(drawerLayout, contentLayout);
? ? }
/**
* 為 DrawerLayout 布局設(shè)置狀態(tài)欄透明(5.0以上半透明效果,不建議使用)
*
? ? * @param activity? ? 需要設(shè)置的activity
? ? * @param drawerLayout DrawerLayout
*/
? ? @Deprecated
? ? public static void setTranslucentForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 設(shè)置狀態(tài)欄透明
? ? ? ? ? ? activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
? ? ? ? ? ? // 設(shè)置內(nèi)容布局屬性
? ? ? ? ? ? ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
? ? ? ? ? ? contentLayout.setFitsSystemWindows(true);
? ? ? ? ? ? contentLayout.setClipToPadding(true);
? ? ? ? ? ? // 設(shè)置抽屜布局屬性
? ? ? ? ? ? ViewGroup vg = (ViewGroup) drawerLayout.getChildAt(1);
? ? ? ? ? ? vg.setFitsSystemWindows(false);
? ? ? ? ? ? // 設(shè)置 DrawerLayout 屬性
? ? ? ? ? ? drawerLayout.setFitsSystemWindows(false);
? ? ? ? }
}
/**
* 為頭部是 ImageView 的界面設(shè)置狀態(tài)欄全透明
*
? ? * @param activity? ? ? 需要設(shè)置的activity
? ? * @param needOffsetView 需要向下偏移的 View
*/
? ? public static void setTransparentForImageView(Activity activity, View needOffsetView) {
setTranslucentForImageView(activity, 0, needOffsetView);
? ? }
/**
* 為頭部是 ImageView 的界面設(shè)置狀態(tài)欄透明(使用默認(rèn)透明度)
*
? ? * @param activity? ? ? 需要設(shè)置的activity
? ? * @param needOffsetView 需要向下偏移的 View
*/
? ? public static void setTranslucentForImageView(Activity activity, View needOffsetView) {
setTranslucentForImageView(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView);
? ? }
/**
* 為頭部是 ImageView 的界面設(shè)置狀態(tài)欄透明
*
? ? * @param activity? ? ? 需要設(shè)置的activity
? ? * @param statusBarAlpha 狀態(tài)欄透明度
? ? * @param needOffsetView 需要向下偏移的 View
*/
? ? public static void setTranslucentForImageView(Activity activity, 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);
? ? ? ? }
}
/**
* 為 fragment 頭部是 ImageView 的設(shè)置狀態(tài)欄透明
*
? ? * @param activity? ? ? fragment 對(duì)應(yīng)的 activity
? ? * @param needOffsetView 需要向下偏移的 View
*/
? ? public static void setTranslucentForImageViewInFragment(Activity activity, View needOffsetView) {
setTranslucentForImageViewInFragment(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView);
? ? }
/**
* 為 fragment 頭部是 ImageView 的設(shè)置狀態(tài)欄透明
*
? ? * @param activity? ? ? fragment 對(duì)應(yīng)的 activity
? ? * @param needOffsetView 需要向下偏移的 View
*/
? ? public static void setTransparentForImageViewInFragment(Activity activity, View needOffsetView) {
setTranslucentForImageViewInFragment(activity, 0, needOffsetView);
? ? }
/**
* 為 fragment 頭部是 ImageView 的設(shè)置狀態(tài)欄透明
*
? ? * @param activity? ? ? fragment 對(duì)應(yīng)的 activity
? ? * @param statusBarAlpha 狀態(tài)欄透明度
? ? * @param needOffsetView 需要向下偏移的 View
*/
? ? public static void setTranslucentForImageViewInFragment(Activity activity, 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);
? ? ? ? }
}
/**
* 隱藏偽狀態(tài)欄 View
*
? ? * @param activity 調(diào)用的 Activity
*/
? ? 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.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);
? ? ? ? }
}
/**
* 添加半透明矩形條
*
? ? * @param activity? ? ? 需要設(shè)置的 activity
? ? * @param statusBarAlpha 透明值
*/
? ? private static void addTranslucentView(Activity activity, 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));
? ? ? ? }
}
/**
* 生成一個(gè)和狀態(tài)欄大小相同的彩色矩形條
*
? ? * @param activity 需要設(shè)置的 activity
? ? * @param color? ? 狀態(tài)欄顏色值
? ? * @return 狀態(tài)欄矩形條
*/
? ? private static ViewcreateStatusBarView(Activity activity, @ColorInt int color) {
return createStatusBarView(activity, color, 0);
? ? }
/**
* 生成一個(gè)和狀態(tài)欄大小相同的半透明矩形條
*
? ? * @param activity 需要設(shè)置的activity
? ? * @param color? ? 狀態(tài)欄顏色值
? ? * @param alpha? ? 透明值
? ? * @return 狀態(tài)欄矩形條
*/
? ? private static ViewcreateStatusBarView(Activity activity, @ColorInt int color, int alpha) {
// 繪制一個(gè)和狀態(tài)欄一樣高的矩形
? ? ? ? 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;
? ? }
/**
* 設(shè)置根布局參數(shù)
*/
? ? 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 (childViewinstanceof ViewGroup) {
childView.setFitsSystemWindows(true);
? ? ? ? ? ? ? ? ((ViewGroup) childView).setClipToPadding(true);
? ? ? ? ? ? }
}
}
/**
* 設(shè)置透明
*/
? ? 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);
? ? ? ? }
}
/**
* 使?fàn)顟B(tài)欄透明
*/
? ? @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);
? ? ? ? }
}
/**
* 創(chuàng)建半透明矩形 View
*
? ? * @param alpha 透明值
? ? * @return 半透明 View
*/
? ? private static ViewcreateTranslucentStatusBarView(Activity activity, int alpha) {
// 繪制一個(gè)和狀態(tài)欄一樣高的矩形
? ? ? ? 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;
? ? }
/**
* 獲取狀態(tài)欄高度
*
? ? * @param context context
? ? * @return 狀態(tài)欄高度
*/
? ? private static int getStatusBarHeight(Context context) {
// 獲得狀態(tài)欄高度
? ? ? ? int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
? ? ? ? return context.getResources().getDimensionPixelSize(resourceId);
? ? }
/**
* 計(jì)算狀態(tài)欄顏色
*
? ? * @param color color值
? ? * @param alpha alpha值
? ? * @return 最終的狀態(tài)欄顏色
*/
? ? 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;
? ? }
}
2.在AndroisManiFest.xml文件application標(biāo)簽設(shè)置theme:
android:theme="@style/TranslucentTheme"
3.在res/values文件夾styles.xml里面配置TranslucentTheme屬性:
<style name="TranslucentTheme" parent="AppTheme">
? ? <item name="windowActionBar">false
? ? <item name="windowNoTitle">true
</style>
4.代碼里面設(shè)置沉填,在onCreate方法setContentView(R.layout.xxx)設(shè)置如下:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
setContentView(R.layout.xxx);
StatusBarUtil.setColorNoTranslucent(this, ContextCompat.getColor(mContext, R.color.main));
QMUIStatusBarHelper.setStatusBarDarkMode(this);//設(shè)置狀態(tài)欄白色字體
5.另外提供其他設(shè)置方法京闰,更多設(shè)置可以看StatusBarUtil工具類(lèi):
/**
* 如果需要圖片侵入狀態(tài)欄,可用此方法
*/
public void setStatusBarImg() {
StatusBarUtil.setTranslucentForCoordinatorLayout(this, 0);
}
/**
* 設(shè)置狀態(tài)欄白色背景翎猛, 黑色字體
*/
public void setStatusBarWhite() {
StatusBarUtil.setColorNoTranslucent(this, ContextCompat.getColor(mContext, R.color.white));
? ? QMUIStatusBarHelper.setStatusBarLightMode(this);//設(shè)置狀態(tài)欄黑色字體
}
到這里開(kāi)發(fā)就結(jié)束了瞭郑,可以去看看效果。