如何解决我有一个固定在底部栏上的 FAB,它的行为有问题
我开发了一个 Android 应用程序,它有一个 Activity
,其中一个 bottomBar
带有一个锚定的 Floating Action Button
。
我的做法是这样的:
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabNewIssue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/strong_blue"
android:contentDescription="Add Issue"
app:srcCompat="@drawable/ic_outline_add_24"
app:tint="@color/white"
app:fabCustomSize="60dp"
app:layout_anchor="@+id/bottomBar"
app:layout_anchorGravity="top|center"/>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
app:fabAlignmentMode="center"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="10dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
app:menu="@menu/bottom_nav_menu"/>
</com.google.android.material.bottomappbar.BottomAppBar>
接下来我要做的是,在某些片段中,我需要隐藏 BottomBar
和 FAB
,我的做法是:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(activity as MainActivity).let {
it.toolbarText = "Olá " + it.user?.name
it.appbar.setExpanded(true)
it.binding.fabNewIssue.visibility = View.GONE
it.binding.bottomBar.visibility = View.GONE
it.binding.mainFragmentContainer.setPadding(0,Utils().convertDpToPixel(60f,requireContext()).toInt())
}
}
我对这种行为没有任何问题,当我尝试返回并再次显示 BottomBar
和 FAB
时出现问题。出现错误,圆条效果消失,如图所示:
First Fragment with normal Bar
Second Fragment with hidden Bar
First Fragment with bugged Bar
有人知道如何解决这个问题吗?
解决方法
尝试将底部栏和 FAB 嵌套在容器中,然后隐藏/显示该布局。
XML 布局:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/bottomBarContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabNewIssue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/strong_blue"
android:contentDescription="Add Issue"
app:srcCompat="@drawable/ic_outline_add_24"
app:tint="@color/white"
app:fabCustomSize="60dp"
app:layout_anchor="@+id/bottomBar"
app:layout_anchorGravity="top|center"/>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
app:fabAlignmentMode="center"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="10dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
app:menu="@menu/bottom_nav_menu"/>
</com.google.android.material.bottomappbar.BottomAppBar>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
活动代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(activity as MainActivity).let {
it.toolbarText = "Olá " + it.user?.name
it.appbar.setExpanded(true)
it.binding.bottomBarContainer.visibility = View.GONE
it.binding.mainFragmentContainer.setPadding(0,Utils().convertDpToPixel(60f,requireContext()).toInt())
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。