We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
public class StatusBarUtil { public static final int DEFAULT_STATUS_BAR_ALPHA = 112; @TargetApi(Build.VERSION_CODES.KITKAT) public static boolean setTranslucentStatus(Activity activity, boolean on) { Window win = activity.getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if (on) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); return on; } /** * 为滑动返回界面设置状态栏颜色 * */ public static void setColorForSwipeBack(Activity activity, @ColorInt int color) { setColorForSwipeBack(activity, color, 0); } /** * 为滑动返回界面设置状态栏颜色 * * @param activity 需要设置的activity * @param color 状态栏颜色值 * @param statusBarAlpha 状态栏透明度 */ 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)); if (contentView == null) return; 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)); } setTranslucentStatus(activity, true); } } /** * 获取状态栏高度 * * @param context context * @return 状态栏高度 */ private static int getStatusBarHeight(Context context) { // 获得状态栏高度 int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); return context.getResources().getDimensionPixelSize(resourceId); } /** * 计算状态栏颜色 * * @param color color值 * @param alpha alpha值 * @return 最终的状态栏颜色 */ private static int calculateStatusColor(@ColorInt int color, int alpha) { 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; } }
public class StatusBarView extends View { public StatusBarView(Context context, AttributeSet attrs) { super(context, attrs); } public StatusBarView(Context context) { super(context); } }
上面的方法改自StatusBarUtil,修改了setTranslucentStatus(activity, true); 根布局如果是CoordinatorLayout,加上android:fitsSystemWindows="true";如果不是,就不要加android:fitsSystemWindows="true",并且需要在根布局设置背景background,不然背景色和状态栏一样(style设置的背景失效,上面的方法相当重新覆盖了背景色)。如果布局有EditText,请用CoordinatorLayout,不然顶不上去
android:fitsSystemWindows="true"
The text was updated successfully, but these errors were encountered:
如果使用转场动画 mSlideBackLayout.isComingToFinish(); overridePendingTransition(R.anim.anim_none, R.anim.anim_slide_out); 要放在super.onBackPressed();前面,不然滑动一下就放开,按返回键退出后,会有背景色盖在前面那个Activity上
@oubowu 你试下上面的沉浸式状态栏方法,应该没有问题的
Sorry, something went wrong.
No branches or pull requests
上面的方法改自StatusBarUtil,修改了setTranslucentStatus(activity, true);
根布局如果是CoordinatorLayout,加上
android:fitsSystemWindows="true"
;如果不是,就不要加android:fitsSystemWindows="true"
,并且需要在根布局设置背景background,不然背景色和状态栏一样(style设置的背景失效,上面的方法相当重新覆盖了背景色)。如果布局有EditText,请用CoordinatorLayout,不然顶不上去The text was updated successfully, but these errors were encountered: