我通过选择 \xe2\x80\x9cBottom Navigation Activity\xe2\x80\x9d 创建了新的 Android 项目。没有其他改变。仅修改\n\xe2\x80\x9cfragment_notifications.xml\xe2\x80\x9d 通过添加背景属性将背景设置为黑色。
\n
我的问题是:
\n感谢您提前抽出时间。
\n我正在使用 Android Studio 4.1.1 并在 AVD Nexus 6 API 26 上运行代码,下面是相关文件的源代码。除通知片段的背景外,全部由 Android Studio 生成。
\n活动_main.xml:
\n<?xml version="1.0" encoding="utf-8"?>\n<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n android:id="@+id/container"\n android:layout_width="match_parent"\n android:layout_height="match_parent"\n android:paddingTop="?attr/actionBarSize">\n\n<com.google.android.material.bottomnavigation.BottomNavigationView\n android:id="@+id/nav_view"\n android:layout_width="0dp"\n android:layout_height="wrap_content"\n android:layout_marginStart="0dp"\n android:layout_marginEnd="0dp"\n android:background="?android:attr/windowBackground"\n app:layout_constraintBottom_toBottomOf="parent"\n app:layout_constraintLeft_toLeftOf="parent"\n app:layout_constraintRight_toRightOf="parent"\n app:menu="@menu/bottom_nav_menu" />\n\n<fragment\n android:id="@+id/nav_host_fragment"\n android:name="androidx.navigation.fragment.NavHostFragment"\n android:layout_width="match_parent"\n android:layout_height="match_parent"\n app:defaultNavHost="true"\n app:layout_constraintBottom_toTopOf="@id/nav_view"\n app:layout_constraintLeft_toLeftOf="parent"\n app:layout_constraintRight_toRightOf="parent"\n app:layout_constraintTop_toTopOf="parent"\n app:navGraph="@navigation/mobile_navigation" />\n\n</androidx.constraintlayout.widget.ConstraintLayout>\nRun Code Online (Sandbox Code Playgroud)\nmobile_navigation.xml如下:
\n<?xml version="1.0" encoding="utf-8"?>\n<navigation xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n xmlns:tools="http://schemas.android.com/tools"\n android:id="@+id/mobile_navigation"\n …Run Code Online (Sandbox Code Playgroud) \xe3\x80\x90update\xe3\x80\x91\n实际上,只需禁用默认的 ActionBar 并为每个片段添加工具栏即可轻松重现该问题。然后您可以看到切换底部目标时工具栏会闪烁。\n
\n请查看https://github.com/025nju/BottomNav上的小演示
\xe3\x80\x90原始帖子\xe3\x80\x91\n我是 Android 新手,我的演示项目是 BottomNav 单活动项目。我检查了很多帖子,看起来很多人都在这种情况下处理工具栏,所有片段都需要有自己的工具栏。
\n在底部选项卡之间切换时,工具栏背景颜色会闪烁。请查看 gif 并忽略最后一个空白帧。\n
如何消除闪烁?\nbtw,当我使用默认的ActionBar时没有出现这样的问题,这是官方不推荐的。\n我在Nexus 6 api26 AVD和真机android版本9上进行了测试。同样的问题。下面是相关代码供大家参考。多谢。
\nFragmentMoney.xml:
\n<?xml version="1.0" encoding="utf-8"?>\n<layout xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n xmlns:tools="http://schemas.android.com/tools">\n<data>\n</data>\n<androidx.constraintlayout.widget.ConstraintLayout\n android:layout_width="match_parent"\n android:layout_height="match_parent"\n tools:context=".ui.money.MoneyFragment">\n\n <include android:id="@+id/fragment_base" layout="@layout/fragment_base"/>\n\n <TextView\n android:id="@+id/text_money"\n ... />\n</androidx.constraintlayout.widget.ConstraintLayout>\nRun Code Online (Sandbox Code Playgroud)\n\nfragment_base.xml 用于工具栏定义:
\n<?xml version="1.0" encoding="utf-8"?>\n<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n android:layout_width="match_parent"\n android:layout_height="match_parent">\n<com.google.android.material.appbar.AppBarLayout\n android:layout_width="match_parent"\n android:layout_height="wrap_content">\n\n <com.google.android.material.appbar.MaterialToolbar\n android:id="@+id/mToolbar"\n android:layout_width="match_parent"\n android:layout_height="?attr/actionBarSize"\n android:background="?attr/colorPrimary"\n android:elevation="4dp"\n android:theme="@style/ThemeOverlay.AppCompat.ActionBar"\n app:popupTheme="@style/ThemeOverlay.AppCompat.Light"\n app:layout_constraintTop_toTopOf="parent">\n </com.google.android.material.appbar.MaterialToolbar>\n\n</com.google.android.material.appbar.AppBarLayout>\n\n</androidx.coordinatorlayout.widget.CoordinatorLayout>\nRun Code Online (Sandbox Code Playgroud)\nMoneyFragment.java:
\npublic View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {\n View root = fragmentMoneyBinding.getRoot();\n ...\n …Run Code Online (Sandbox Code Playgroud) android-fragments android-toolbar bottomnavigationview android-jetpack