当前位置:   article > 正文

沉浸式状态栏,状态栏调色_windowinsetsnavhostfragment

windowinsetsnavhostfragment

工具类

  1. import android.app.Activity;
  2. import android.graphics.Color;
  3. import android.os.Build;
  4. import android.view.View;
  5. import android.view.Window;
  6. import android.view.WindowManager;
  7. public class StatusBar {
  8. /**
  9. * 6.0以上的沉浸式布局
  10. * 6.0以上才可以修改字体颜色
  11. *
  12. * @param activity Activity
  13. */
  14. public static void fitSystemBar(Activity activity) {
  15. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  16. return;
  17. }
  18. Window window = activity.getWindow();
  19. View decorView = window.getDecorView();
  20. // 1.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN--能够使得我们的页面布局延伸到状态栏之下,但不会隐藏状态栏,
  21. // 也就相当于状态栏是遮盖在布局之上的,两者重叠
  22. // View.SYSTEM_UI_FLAG_FULLSCREEN -- 能够使得我们的页面布局延伸到状态栏,但是会隐藏状态栏。状态栏不显示,
  23. // View.SYSTEM_UI_FLAG_FULLSCREEN 作用等同于 WindowManager.LayoutParams.FLAG_FULLSCREEN
  24. // 2.View.SYSTEM_UI_FLAG_LAYOUT_STABLE 稳定的布局,兼顾虚拟导航键
  25. // 3.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 亮色的状态栏(白底黑字)
  26. decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
  27. View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
  28. View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
  29. // 4.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,允许对状态栏开启绘制
  30. window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  31. window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  32. // 5.指定状态栏颜色,默认是灰色
  33. window.setStatusBarColor(Color.TRANSPARENT);
  34. }
  35. /**
  36. * 6.0及以上的状态栏色调
  37. *
  38. * @param activity
  39. * @param light true:白底黑字,false:黑底白字
  40. */
  41. public static void lightStatusBar(Activity activity, boolean light) {
  42. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  43. return;
  44. }
  45. Window window = activity.getWindow();
  46. View decorView = window.getDecorView();
  47. int visibility = decorView.getSystemUiVisibility();
  48. if (light) {
  49. visibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
  50. } else {
  51. visibility &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
  52. }
  53. decorView.setSystemUiVisibility(visibility);
  54. }
  55. }

使用:

StatusBar.fitSystemBar(this)

同时,需要在调用页面的根布局中加入属性:

    android:fitsSystemWindows="true"

如果在 NavHostFragment 中使用沉浸式状态栏需要重写 NavHostFragment 中的方法:

  1. import android.os.Bundle;
  2. import android.view.LayoutInflater;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import androidx.annotation.NonNull;
  6. import androidx.annotation.Nullable;
  7. import androidx.navigation.fragment.NavHostFragment;
  8. public class WindowInsetsNavHostFragment extends NavHostFragment {
  9. @Nullable
  10. @Override
  11. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  12. WindowInsetsFrameLayout layout = new WindowInsetsFrameLayout(inflater.getContext());
  13. layout.setId(getId());
  14. return layout;
  15. }
  16. }

同时重写FrameLayout中的方法:

  1. import android.content.Context;
  2. import android.util.AttributeSet;
  3. import android.view.View;
  4. import android.view.WindowInsets;
  5. import android.widget.FrameLayout;
  6. import androidx.annotation.NonNull;
  7. import androidx.annotation.Nullable;
  8. public class WindowInsetsFrameLayout extends FrameLayout {
  9. public WindowInsetsFrameLayout(@NonNull Context context) {
  10. super(context);
  11. }
  12. public WindowInsetsFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
  13. super(context, attrs);
  14. }
  15. public WindowInsetsFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  16. super(context, attrs, defStyleAttr);
  17. }
  18. public WindowInsetsFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  19. super(context, attrs, defStyleAttr, defStyleRes);
  20. }
  21. /**
  22. * 请求重新分发布局事件
  23. * @param child View
  24. */
  25. @Override
  26. public void addView(View child) {
  27. super.addView(child);
  28. requestApplyInsets();
  29. }
  30. /**
  31. * 重写dispatchApplyWindowInsets方法,让事件全部都分发
  32. *
  33. * @param insets WindowInsets
  34. * @return WindowInsets
  35. */
  36. @Override
  37. public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
  38. WindowInsets windowInsets = super.dispatchApplyWindowInsets(insets);
  39. if (!insets.isConsumed()) {
  40. int childCount = getChildCount();
  41. for (int i = 0; i < childCount; i++) {
  42. windowInsets = getChildAt(i).dispatchApplyWindowInsets(insets);
  43. }
  44. }
  45. return windowInsets;
  46. }
  47. }

原因是因为沉浸式布局应用在 NavHostFragment 中时,由于添加的Fragment只会分发一次重新布局的事件,只会有第一个Fragment有效果,重写上面方法会使得所有Fragment都比分发到事件

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/254068
推荐阅读
相关标签
  

闽ICP备14008679号