g g*_*g g 2 android-fragments android-toolbar bottomnavigationview android-jetpack
\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 Toolbar mToolbar = (Toolbar) root.findViewById(R.id.mToolbar);\n mToolbar.setTitle(R.string.title_money);\n ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);\n return root;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\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"\nxmlns:app="http://schemas.android.com/apk/res-auto"\nxmlns:tools="http://schemas.android.com/tools"\nandroid:id="@+id/container"\nandroid:layout_width="match_parent"\nandroid:layout_height="match_parent">\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="0dp"\n app:defaultNavHost="true"\n app:layout_constraintBottom_toTopOf="@+id/nav_view"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toTopOf="parent"\n app:navGraph="@navigation/mobile_navigation" />\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_constraintTop_toBottomOf="@id/nav_host_fragment"\n app:layout_constraintLeft_toLeftOf="parent"\n app:layout_constraintRight_toRightOf="parent"\n app:menu="@menu/bottom_nav_menu" />\n\n<com.google.android.material.floatingactionbutton.FloatingActionButton\n android:id="@+id/floatingActionButton"\n ... />\n\n</androidx.constraintlayout.widget.ConstraintLayout>\nRun Code Online (Sandbox Code Playgroud)\nMainActivity.java:
\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_main);\n BottomNavigationView navView = findViewById(R.id.nav_view);\n appBarConfiguration = new AppBarConfiguration.Builder(\n R.id.navigation_note, R.id.navigation_money, R.id.navigation_time)\n .build();\n navController = Navigation.findNavController(this, R.id.nav_host_fragment);\n NavigationUI.setupWithNavController(navView, navController);\n}\n...\n}\nRun Code Online (Sandbox Code Playgroud)\n
您必须禁用(替换)片段之间的动画。
创建空动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--Empty to disable animation-->
</set>
Run Code Online (Sandbox Code Playgroud)
并将其放入以下文件中:
res/animator/nav_default_enter_anim.xml
res/animator/nav_default_exit_anim.xml
res/animator/nav_default_pop_enter_anim.xml
res/animator/nav_default_pop_exit_anim.xml
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1007 次 |
| 最近记录: |