Fab 没有被底部导航上方的小吃栏推上来

问题描述 投票:0回答:2

(这个错误是在一个大项目中发生的,所以我做了一个小错误以便于演示)

我已经被这个问题困扰了一段时间,但我在任何地方都找不到答案

~布局

我想在显示 Snackbar 时将 fab 向上推。当底部导航栏被移除并显示一个小吃栏时,晶圆厂正在被推起。但是当我插入底部导航栏时,会出现小吃栏,但 fab 并没有被推上来

<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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:layout_anchor="@id/bar"
        app:layout_anchorGravity="bottom|end" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:background="@color/design_default_color_secondary"
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_insetEdge="bottom"
        android:layout_gravity="bottom"
        app:menu="@menu/menu"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button = findViewById<Button>(R.id.button)
        button.setOnClickListener {
            Snackbar
                .make(it, "tttt", Snackbar.LENGTH_LONG)
                .setAnchorView(findViewById(R.id.bar))
                .show()
        }
    }
}

提前致谢:)

android floating-action-button android-coordinatorlayout
2个回答
4
投票

将底部导航移动到另一个布局并将 CoordinatorLayout 包裹在线性布局中,现在小吃栏应该以 CoordinatorLayout 作为根,CoordinatorLayout 将在底部放置 fab,所以当小吃到来时它会抬起它。

<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/snackRoot"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="16dp"
            android:src="@drawable/ic_android_circle" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/design_default_color_secondary"
        app:layout_insetEdge="bottom"
        app:menu="@menu/menu" />

</LinearLayout>

显示小吃店

Snackbar
.make(activity.findViewById(R.id.snackRoot),
"tttt", 
Snackbar.LENGTH_LONG)
.show()

0
投票

这似乎成功解决了这个问题:

snackbar.addCallback(object : Snackbar.Callback() {
    override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
        transientBottomBar?.removeCallback(this)
        Handler(Looper.getMainLooper()).post {
            coordinatorLayout?.requestApplyInsets()
        }
    }
})
© www.soinside.com 2019 - 2024. All rights reserved.