bottomappbar android中的导航抽屉

Vip*_*bey 6 android navigation-drawer

在此处输入图片说明

class BottomNavigationDrawerFragment: BottomSheetDialogFragment(), 
   NavigationView.OnNavigationItemSelectedListener {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_bottomsheet, container, false)
    }

    override fun onNavigationItemSelected(item : MenuItem): Boolean {
        // Bottom Navigation Drawer menu item clicks
        when (item.itemId) {
            R.id.nav1 -> context!!.toast("oneeeeee")
            R.id.nav2 -> context!!.toast("twoooooo")
            R.id.nav3 -> context!!.toast("threeeee")

            return true
        }
        // Add code here to update the UI based on the item selected
        // For example, swap
    }
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        navigation_view.setNavigationItemSelectedListener(this)

        // Add code here to update the UI based on the item selected
        // For example, swap
    }
}

// This is an extension method for easy Toast call
fun Context.toast(message: CharSequence) {
    val toast = Toast.makeText(this, message, Toast.LENGTH_SHORT)
    toast.setGravity(Gravity.BOTTOM, 0, 600)
    toast.show()
}
Run Code Online (Sandbox Code Playgroud)

我想要实现的是图像中给出的东西。我想制作一个navigation drawer底部应用栏。上面的代码不起作用,它告诉 unresolved reference type setNavigationItemSelectedListener。我的代码中有什么错误?

Abh*_*pta 1

请参阅此代码,它有一个 navigationIcon 属性,但您可以用作底部应用程序栏。如果您需要在此点击上导航抽屉,那么您必须由我们自己定制。

<com.google.android.material.bottomappbar.BottomAppBar
  android:id="@+id/bottom_app_bar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="bottom"
  app:elevation="5dp"
  android:elevation="5dp"
  app:fabAttached="true"
  app:fabCradleDiameter="0dp"
  app:backgroundTint="@color/colorPrimary"
  app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
  app:fabAlignmentMode="center"
  app:menu="@menu/bottom_bar_menu"/>
Run Code Online (Sandbox Code Playgroud)

在res>menu>bottom_bar_menu中,将showAsAction更改为always或ifRoom,为action_settings放置一个图标并删除orderInCategory

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:showAsAction="always"
        android:icon="" />
    <item
        android:title="@string/search"
        android:id="@+id/search"
        android:icon="@drawable/ic_search_black_24dp"
        android:showAsAction="always" />

    <item
        android:id="@+id/app_bar_archieve"
        android:icon="@drawable/ic_bottom_bar_hamburger" // navigation icon
        android:title="@string/action_archieve"
        app:showAsAction="ifRoom"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

在java中:

BottomAppBar bar = (BottomAppBar) findViewById(R.id.bar);
setSupportActionBar(bar);

bar.setNavigationOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // Handle the navigation click by showing a BottomDrawer etc.
    }
});

bar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        // Handle actions based on the menu item
        return true;
    }
});
Run Code Online (Sandbox Code Playgroud)

参考链接: https: //material.io/develop/android/components/bottom-app-bar/