• 安卓在Fragment控制状态栏显示隐藏


    废话不多上效果

    隐藏

    显示

    核心代码

    首先是Framgrent

    1. package com.zx.tab;
    2. import android.content.Context;
    3. import android.os.Bundle;
    4. import android.view.LayoutInflater;
    5. import android.view.View;
    6. import android.view.ViewGroup;
    7. import android.widget.Button;
    8. import androidx.annotation.NonNull;
    9. import androidx.annotation.Nullable;
    10. import androidx.fragment.app.Fragment;
    11. /**
    12. * 首页Fragment,用于展示和控制状态栏的显示与隐藏。
    13. */
    14. public class HomeFragment extends Fragment {
    15. private static final String ARG_PARAM1 = "首页";
    16. private Button btn_hide_status_bar; // 控制隐藏状态栏的按钮
    17. private Button btn_show_status_bar; // 控制显示状态栏的按钮
    18. // 数据传递监听器接口
    19. public interface OnDataPassListener {
    20. void onHideStatusBar(String data); // 当隐藏状态栏时被调用
    21. void onShowStatusBar(String data); // 当显示状态栏时被调用
    22. }
    23. // 用于接收Activity传回的监听实例
    24. private OnDataPassListener mDataPassListener;
    25. // 创建HomeFragment实例的方法,用于传入参数
    26. public static HomeFragment newInstance(String param1) {
    27. HomeFragment fragment = new HomeFragment();
    28. Bundle args = new Bundle();
    29. args.putString(ARG_PARAM1, param1);
    30. fragment.setArguments(args);
    31. return fragment;
    32. }
    33. @Nullable
    34. @Override
    35. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    36. View contentView = inflater.inflate(R.layout.home, container, false);
    37. btn_hide_status_bar = contentView.findViewById(R.id.btn_hide_status_bar);
    38. btn_show_status_bar = contentView.findViewById(R.id.btn_show_status_bar);
    39. // 设置按钮点击事件
    40. btn_hide_status_bar.setOnClickListener(v -> {
    41. if (mDataPassListener != null) {
    42. mDataPassListener.onHideStatusBar("状态栏已隐藏");
    43. }
    44. });
    45. btn_show_status_bar.setOnClickListener(v -> {
    46. if (mDataPassListener != null) {
    47. mDataPassListener.onShowStatusBar("状态栏已显示");
    48. }
    49. });
    50. return contentView;
    51. }
    52. // 在Fragment与Activity建立关联时设置监听器
    53. @Override
    54. public void onAttach(@NonNull Context context) {
    55. super.onAttach(context);
    56. try {
    57. mDataPassListener = (OnDataPassListener) context;
    58. } catch (ClassCastException e) {
    59. throw new ClassCastException(context.toString() + " 必须实现 OnDataPassListener 接口");
    60. }
    61. }
    62. }

    布局

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:orientation="vertical"
    5. android:layout_height="match_parent">
    6. <Button
    7. android:layout_width="match_parent"
    8. android:layout_height="wrap_content"
    9. android:text="隐藏状态栏"
    10. android:id="@+id/btn_hide_status_bar"/>
    11. <Button
    12. android:id="@+id/btn_show_status_bar"
    13. android:layout_width="match_parent"
    14. android:layout_height="wrap_content"
    15. android:text="显示状态栏"/>
    16. LinearLayout>

    布局效果

    最后这里是Activity代码

    1. package com.zx.tab;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import androidx.fragment.app.Fragment;
    4. import androidx.viewpager2.widget.ViewPager2;
    5. import android.os.Build;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.view.WindowInsets;
    9. import android.view.WindowInsetsController;
    10. import android.view.WindowManager;
    11. import android.widget.ImageView;
    12. import android.widget.LinearLayout;
    13. import android.widget.TextView;
    14. import android.widget.Toast;
    15. import java.util.ArrayList;
    16. import java.util.List;
    17. public class MainActivity extends AppCompatActivity implements HomeFragment.OnDataPassListener{
    18. private final List fragmentList = new ArrayList<>();
    19. private final int[] tabIds = {
    20. R.id.ll_home,
    21. R.id.ll_mine
    22. };
    23. private final int[] iconIds = {
    24. R.id.im_home,
    25. R.id.im_mine
    26. };
    27. private final int[] textIds = {
    28. R.id.tv_home,
    29. R.id.tv_mine
    30. };
    31. private ViewPager2 viewPager2=null;
    32. private ImageView[] tabIcons=null;
    33. private TextView[] tabTexts=null;
    34. private ImageView imCurrent=null;
    35. private TextView tvCurrent=null;
    36. private LinearLayout ll_tabbar;
    37. @Override
    38. protected void onCreate(Bundle savedInstanceState) {
    39. super.onCreate(savedInstanceState);
    40. setContentView(R.layout.activity_main);
    41. this.initializeViews();
    42. this.initViewPager();
    43. this.changeTab(0);
    44. }
    45. private void initializeViews() {
    46. this.viewPager2 = findViewById(R.id.viewPager2);
    47. this.ll_tabbar=findViewById(R.id.ll_tabbar);
    48. LinearLayout[] tabLayouts = new LinearLayout[2];
    49. this.tabIcons = new ImageView[2];
    50. this. tabTexts = new TextView[2];
    51. for (int i = 0; i < 2; i++) {
    52. tabLayouts[i] = findViewById(tabIds[i]);
    53. tabIcons[i] = findViewById(iconIds[i]);
    54. tabTexts[i] = findViewById(textIds[i]);
    55. final int index = i;
    56. tabLayouts[i].setOnClickListener(view -> {
    57. viewPager2.setCurrentItem(index, false);
    58. changeTab(index);
    59. });
    60. }
    61. }
    62. private void initViewPager() {
    63. this. fragmentList.add(HomeFragment.newInstance(getString(R.string.tv_home)));
    64. this.fragmentList.add(MeFragment.newInstance(getString(R.string.tv_mine)));
    65. this.viewPager2.setAdapter(new FragmentAdapter(getSupportFragmentManager(), getLifecycle(), fragmentList));
    66. this. viewPager2.setUserInputEnabled(false);//是否滑动
    67. this.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
    68. @Override
    69. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    70. super.onPageScrolled(position, positionOffset, positionOffsetPixels);
    71. changeTab(position);
    72. }
    73. });
    74. }
    75. private void changeTab(int position) {
    76. if (imCurrent != null && tvCurrent != null) {
    77. this.imCurrent.setSelected(false);
    78. this.tvCurrent.setSelected(false);
    79. }
    80. this.imCurrent = tabIcons[position];
    81. this.tvCurrent = tabTexts[position];
    82. this.imCurrent.setSelected(true);
    83. this.tvCurrent.setSelected(true);
    84. }
    85. //实现接口 隐藏状态栏
    86. public void onHideStatusBar(String data) {
    87. this.ll_tabbar.setVisibility(View.GONE);
    88. // 隐藏顶部状态栏
    89. if (getSupportActionBar() != null) {
    90. getSupportActionBar().hide();
    91. }
    92. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    93. // 使用WindowInsetsController的动画
    94. WindowInsetsController insetsController = getWindow().getInsetsController();
    95. if (insetsController != null) {
    96. insetsController.hide(WindowInsets.Type.statusBars());
    97. insetsController.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    98. }
    99. } else {
    100. // 兼容旧版本,使用系统UI标志,并添加淡出动画
    101. getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    102. getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
    103. | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    104. | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    105. }
    106. }
    107. // 实现接口 显示状态栏
    108. public void onShowStatusBar(String data) {
    109. this.ll_tabbar.setVisibility(View.VISIBLE);
    110. // 显示顶部状态栏
    111. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    112. WindowInsetsController insetsController = getWindow().getInsetsController();
    113. if (insetsController != null) {
    114. insetsController.show(WindowInsets.Type.statusBars());
    115. insetsController.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    116. }
    117. } else {
    118. getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    119. getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    120. // 同样,这里可以考虑使用淡入动画来平滑显示状态栏
    121. }
    122. if (getSupportActionBar() != null) {
    123. getSupportActionBar().show();
    124. }
    125. // 如果状态栏颜色需要透明处理,可以在显示时设置(这取决于您的UI设计)
    126. // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    127. // 或者使用setStatusBarColor设置特定颜色
    128. }
    129. }

    下面是布局

    1. "1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:background="@color/white"
    6. android:orientation="vertical">
    7. <androidx.viewpager2.widget.ViewPager2
    8. android:id="@+id/viewPager2"
    9. android:layout_width="match_parent"
    10. android:layout_height="0dp"
    11. android:layout_weight="1" />
    12. <View
    13. android:layout_width="match_parent"
    14. android:layout_height="0.1dp"
    15. android:background="#f0f0f0" />
    16. <LinearLayout
    17. android:id="@+id/ll_tabbar"
    18. android:layout_width="match_parent"
    19. android:layout_height="56dp"
    20. android:background="@color/white"
    21. android:orientation="horizontal">
    22. <LinearLayout
    23. android:id="@+id/ll_home"
    24. android:layout_width="0dp"
    25. android:layout_height="match_parent"
    26. android:layout_weight="1"
    27. android:gravity="center"
    28. android:orientation="vertical">
    29. <ImageView
    30. android:id="@+id/im_home"
    31. android:layout_width="24dp"
    32. android:layout_height="24dp"
    33. android:background="@drawable/tab_menu_home" />
    34. <TextView
    35. android:id="@+id/tv_home"
    36. android:layout_width="wrap_content"
    37. android:layout_height="wrap_content"
    38. android:text="@string/tv_home"
    39. android:textColor="@drawable/tabar_title_text"
    40. android:textSize="12sp" />
    41. LinearLayout>
    42. <LinearLayout
    43. android:id="@+id/ll_mine"
    44. android:layout_width="0dp"
    45. android:layout_height="56dp"
    46. android:layout_weight="1"
    47. android:gravity="center"
    48. android:orientation="vertical">
    49. <ImageView
    50. android:id="@+id/im_mine"
    51. android:layout_width="24dp"
    52. android:layout_height="24dp"
    53. android:background="@drawable/tab_menu_mine" />
    54. <TextView
    55. android:id="@+id/tv_mine"
    56. android:layout_width="wrap_content"
    57. android:layout_height="wrap_content"
    58. android:text="@string/tv_mine"
    59. android:textColor="@drawable/tabar_title_text"
    60. android:textSize="12sp" />
    61. LinearLayout>
    62. LinearLayout>
    63. LinearLayout>

    效果

    以上就是 安卓在Fragment控制状态栏显示隐藏的代码,

    下面是dome地址

    安卓在Fragment控制状态栏显示隐藏资源-CSDN文库

  • 相关阅读:
    PHP中interface关键字
    介绍ServiceSelf项目
    P5 直升 P7!“阿里”最新出品年薪 30W~120WJava 架构师学习路线
    第53节——Redux Toolkit初识
    凭什么Java程序员的工资那么高,原来要“啃透”这999页阿里P7学习笔记
    解决计算机丢失msvcr71.dll问题,总结5种解决方法分享
    字符型注入([SWPUCTF 2021 新生赛]easy_sql)
    10个PyCharm常用的免费插件,让开发迅速飙升
    dreamweaver家乡主题网页设计 DIV布局个人介绍网页模板代码 DW学生个人网站制作成品下载 HTML5期末大作业
    【深度学习】常用算法生成对抗网络、自编码网络、多层感知机、反向传播等讲解(图文解释 超详细)
  • 原文地址:https://blog.csdn.net/qq_41733851/article/details/139728077