You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Material3中如何让OptionsMenu显示在ActionBar下方?

Android Material3中如何让OptionsMenu显示在ActionBar下方?

嘿,我之前在做Material3 Android应用的时候,也纠结过怎么让选项菜单乖乖显示在ActionBar(也就是MaterialToolbar)下方,刚好踩过坑,给你分享两个靠谱的方法,完全贴合官方的设计要求:

方法一:用Material3官方推荐的MaterialDropdownMenu组件

这种方式是最贴合Material3设计规范的,直接把菜单锚定在Toolbar上,就能精准显示在它的正下方:

  1. 先在布局文件里把MaterialToolbar和MaterialDropdownMenu搭配好:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Material3顶部工具栏 -->
    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/topAppBar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:title="我的应用" />

    <!-- Material3下拉菜单,默认隐藏,锚定到Toolbar -->
    <com.google.android.material.dropdown.MaterialDropdownMenu
        android:id="@+id/dropdownMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:menu="@menu/your_menu_list"
        app:anchorView="@id/topAppBar" />
</LinearLayout>
  1. 接着在Activity里给Toolbar的溢出按钮设置点击事件,触发菜单显示:
val topAppBar = findViewById<MaterialToolbar>(R.id.topAppBar)
val dropdownMenu = findViewById<MaterialDropdownMenu>(R.id.dropdownMenu)

// 给Toolbar添加上溢出菜单
topAppBar.inflateMenu(R.menu.your_menu_list)

// 监听菜单点击,让下拉菜单显示在Toolbar下方
topAppBar.setOnMenuItemClickListener { menuItem ->
    when (menuItem.itemId) {
        // 假设你的溢出菜单id是action_overflow
        R.id.action_overflow -> {
            dropdownMenu.show()
            true
        }
        // 处理其他菜单项的点击逻辑
        R.id.action_setting -> {
            // 比如打开设置页面
            true
        }
        else -> false
    }
}

方法二:调整传统PopupMenu的锚点实现

如果你更习惯用传统的PopupMenu,也可以直接把它的锚点设置为Toolbar,这样弹出的位置就会自动在Toolbar下方:

val topAppBar = findViewById<MaterialToolbar>(R.id.topAppBar)
topAppBar.inflateMenu(R.menu.your_menu_list)

topAppBar.setOnMenuItemClickListener { menuItem ->
    when (menuItem.itemId) {
        R.id.action_more -> {
            // 创建PopupMenu,锚点绑定到topAppBar,确保显示在它下方
            val popupMenu = PopupMenu(this, topAppBar)
            popupMenu.inflate(R.menu.your_menu_list)
            // 可选:设置显示菜单图标
            popupMenu.setForceShowIcon(true)
            // 监听菜单项点击
            popupMenu.setOnMenuItemClickListener { item ->
                when (item.itemId) {
                    R.id.action_share -> {
                        // 处理分享逻辑
                        true
                    }
                    else -> false
                }
            }
            popupMenu.show()
            true
        }
        else -> super.onOptionsItemSelected(menuItem)
    }
}

最后要提醒一句,不管用哪种方法,都要确保你的项目已经引入了Material3的依赖,比如在Module级别的build.gradle里加上:

dependencies {
    implementation 'com.google.android.material:material:1.12.0' // 用最新版本就好
}

备注:内容来源于stack exchange,提问作者BenjyTec

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 17:44:37