Android : 底部导航顶部的浮动操作栏不起作用

Android : Floating Action Bar on the Top of Bottom Navigation is Not Working

在我的应用程序中,我放置了一个底部导航栏,还添加了带有底部导航栏高度的浮动按钮

然后我放置了一个 Toast Msg 来检查浮动按钮是否工作但它不工作

bottom_menu_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/bottom_nav_menu" />


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:src="@drawable/ic_add"
        android:tint="@color/colorWhite"
        app:backgroundTint="@color/colorPrimary"
        app:layout_anchor="@id/bottom_nav"
        app:elevation="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

在我的 activity_main.xml 中,我包含了一个 bottom_menu_bar.xml 文件,我在 activity_main.xml

中包含了我的 Secondactivity.xml 文件
<RelativeLayout

    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:layout_alignParentTop="false"
    tools:context=".MainActivity">

<include
    android:id="@+id/empty_list"
    layout="@layout/empty_list" />

    <include
        android:id="@+id/bottom_nav"
        layout="@layout/bottom_menu_bar"
      />

MainActivity.Java

public class MainActivity extends AppCompatActivity {

  

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatingActionButton);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "FloatingPlus Button was Clicked ", Toast.LENGTH_SHORT).show();
              
            }
        });
    }
}

在你的 fab 按钮中添加这两行

android:clickable="true"
    android:focusable="true"

删除 mainactivity.xml

中的 empty_list