赞
踩
工具类
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Build;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
-
- public class StatusBar {
- /**
- * 6.0以上的沉浸式布局
- * 6.0以上才可以修改字体颜色
- *
- * @param activity Activity
- */
- public static void fitSystemBar(Activity activity) {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
- return;
- }
- Window window = activity.getWindow();
- View decorView = window.getDecorView();
- // 1.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN--能够使得我们的页面布局延伸到状态栏之下,但不会隐藏状态栏,
- // 也就相当于状态栏是遮盖在布局之上的,两者重叠
- // View.SYSTEM_UI_FLAG_FULLSCREEN -- 能够使得我们的页面布局延伸到状态栏,但是会隐藏状态栏。状态栏不显示,
- // View.SYSTEM_UI_FLAG_FULLSCREEN 作用等同于 WindowManager.LayoutParams.FLAG_FULLSCREEN
- // 2.View.SYSTEM_UI_FLAG_LAYOUT_STABLE 稳定的布局,兼顾虚拟导航键
- // 3.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 亮色的状态栏(白底黑字)
- decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
- View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
- View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
- // 4.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,允许对状态栏开启绘制
- window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
- window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- // 5.指定状态栏颜色,默认是灰色
- window.setStatusBarColor(Color.TRANSPARENT);
- }
-
- /**
- * 6.0及以上的状态栏色调
- *
- * @param activity
- * @param light true:白底黑字,false:黑底白字
- */
- public static void lightStatusBar(Activity activity, boolean light) {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
- return;
- }
- Window window = activity.getWindow();
- View decorView = window.getDecorView();
- int visibility = decorView.getSystemUiVisibility();
- if (light) {
- visibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
- } else {
- visibility &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
- }
- decorView.setSystemUiVisibility(visibility);
- }
- }
使用:
StatusBar.fitSystemBar(this)
同时,需要在调用页面的根布局中加入属性:
android:fitsSystemWindows="true"
如果在 NavHostFragment 中使用沉浸式状态栏需要重写 NavHostFragment 中的方法:
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
-
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import androidx.navigation.fragment.NavHostFragment;
-
- public class WindowInsetsNavHostFragment extends NavHostFragment {
- @Nullable
- @Override
- public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- WindowInsetsFrameLayout layout = new WindowInsetsFrameLayout(inflater.getContext());
- layout.setId(getId());
- return layout;
- }
- }
同时重写FrameLayout中的方法:
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.View;
- import android.view.WindowInsets;
- import android.widget.FrameLayout;
-
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
-
- public class WindowInsetsFrameLayout extends FrameLayout {
- public WindowInsetsFrameLayout(@NonNull Context context) {
- super(context);
- }
-
- public WindowInsetsFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- }
-
- public WindowInsetsFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
-
- public WindowInsetsFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
- super(context, attrs, defStyleAttr, defStyleRes);
- }
-
- /**
- * 请求重新分发布局事件
- * @param child View
- */
- @Override
- public void addView(View child) {
- super.addView(child);
- requestApplyInsets();
- }
-
- /**
- * 重写dispatchApplyWindowInsets方法,让事件全部都分发
- *
- * @param insets WindowInsets
- * @return WindowInsets
- */
- @Override
- public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
- WindowInsets windowInsets = super.dispatchApplyWindowInsets(insets);
- if (!insets.isConsumed()) {
- int childCount = getChildCount();
- for (int i = 0; i < childCount; i++) {
- windowInsets = getChildAt(i).dispatchApplyWindowInsets(insets);
- }
- }
- return windowInsets;
- }
- }
原因是因为沉浸式布局应用在 NavHostFragment 中时,由于添加的Fragment只会分发一次重新布局的事件,只会有第一个Fragment有效果,重写上面方法会使得所有Fragment都比分发到事件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。