简体   繁体   English

底部 AppBar,两侧带有图标

[英]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.默认情况下,以 fab 为中心,所有图标都向右对齐,但我需要在一侧和另一侧有 3 个图标,如下例所示。

在此处输入图片说明

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.我膨胀了 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: 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: 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)
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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