【发布时间】:2019-05-20 07:25:22
【问题描述】:
我正在创建一个带有浮动操作按钮的Bottom App Bar。 App Bar 是我的 Main Activity 的一部分,我想隐藏 Activity 中某些 Fragments 的 FAB。
我已经尝试过“View.GONE”和“fab.hide()”。然后我尝试使用以下功能隐藏 Fab:
private fun hideFloatingActionButton() {
val params = fab.layoutParams as CoordinatorLayout.LayoutParams
val behavior = params.behavior as FloatingActionButton.Behavior?
if (behavior != null) {
behavior.isAutoHideEnabled = false
}
params.anchorId = View.NO_ID;
params.width = 0;
params.height = 0;
fab.layoutParams = params
fab.hide()
}
我的 layout.xml:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:background="@color/backPrimary"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
content
...
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
style="@style/Widget.MaterialComponents.BottomAppBar"
app:navigationIcon="@drawable/ic_burger_menu"
app:fabAlignmentMode="end"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start_project_day"
app:srcCompat="@drawable/ic_next"
app:layout_anchor="@id/bar"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
【问题讨论】:
-
什么时候调用
hideFloatingActionButton方法 -
我正在使用导航组件。当片段启动时,我会向 Activity 发送一个事件,并在必要时从 Activity 调用“hideFloatingActionButton()”。
标签: android android-layout kotlin