简体   繁体   中英

Bottom AppBar with icons on both sides

By default, with fab in the center, all icons are aligned to the right, but I need to have 3 icons on one side and the other, like in the example below.

在此处输入图片说明

The screenshot shows a click on one of the buttons, such a ripple is in the bottom appbar (not in the bottom navigation), a toast also appears when clicked, this can only be with a non-fake menuitem, and not for example an imageButton.

It is important that each icon is a real menuitem (not an imageButton), a long press on the menuitem should display a toast over the icon (as you can see in the screenshot), which is very important. I would be extremely grateful for your help!

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        app:showAsAction="always"
        android:id="@+id/menu_home"
        android:layout_alignParentRight="true"
        android:icon="@drawable/ic_home"
        android:title="@string/title_home" />

    <item
        app:showAsAction="always"
        android:id="@+id/menu_tasks"
        android:icon="@drawable/ic_tasks"
        android:title="@string/title_tasks" />

    <item
        app:showAsAction="always"
        android:id="@+id/menu_habits"
        android:icon="@drawable/ic_timer"
        android:title="@string/title_pomo" />

    <item
        android:id="@+id/menu_timer"
        app:showAsAction="always"
        android:icon="@drawable/ic_habit"
        android:title="@string/title_habits" />
</menu>

Current result:

在此处输入图片说明

I did it! I inflated the left side of the menu in the ActionMenuView. 在此处输入图片说明

In layout:

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottomAppBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:contentInsetStart="0.0dip"
    android:contentInsetLeft="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:menu="@menu/bottom_nav_menu_right">

    <androidx.appcompat.widget.ActionMenuView
        android:id="@+id/additional_menu"
        android:layout_width="wrap_content"
        android:layout_height="?actionBarSize" />
</com.google.android.material.bottomappbar.BottomAppBar>

bottom_nav_menu_left:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/menu_tasks"
        android:layout_alignParentRight="true"
        android:icon="@drawable/ic_tasks"
        android:title="tasks"
        app:showAsAction="always" />

    <item
        android:id="@+id/menu_habit"
        android:layout_alignParentRight="true"
        android:icon="@drawable/ic_habit"
        android:title="habits"
        app:showAsAction="always" />

    <item
        android:id="@+id/menu_add"
        android:layout_alignParentRight="true"
        android:icon="@drawable/ic_add"
        android:title="add"
        app:showAsAction="always" />
</menu>

bottom_nav_menu_right:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/menu_nav"
        android:layout_alignParentRight="true"
        android:icon="@drawable/ic_menu"
        android:title="menu"
        app:showAsAction="always" />

    <item
        android:id="@+id/menu_home"
        android:layout_alignParentRight="true"
        android:icon="@drawable/ic_home"
        android:title="home"
        app:showAsAction="always" />

    <item
        android:id="@+id/menu_timer"
        android:layout_alignParentRight="true"
        android:icon="@drawable/ic_timer"
        android:title="timer"
        app:showAsAction="always" />
</menu>

Inflating menu with left icons:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
        val actionMenuView = findViewById<ActionMenuView>(...)
        menuInflater.inflate(R.menu.bottom_nav_menu_left, actionMenuView.menu)
        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM