背景
我在操作栏(实际上是工具栏)中有一个菜单项,单击它时会显示可供选择的项目列表,类似于单选按钮:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon="@drawable/..."
android:title="@string/..."
app:showAsAction="always">
<menu>
<group
android:id="@+id/..."
android:checkableBehavior="single">
<item .../>
<item .../>
<item .../>
</group>
</menu>
</item>
</menu>
我需要在这个项目列表下方放置一个项目,这将在它和列表之间有一个分隔符。类似于 Material 设计指南显示的内容(取自 here):
编辑:这是我想要做的草图:
问题
我找不到办法。
我尝试过的
我发现的唯一可能的解决方案是:
更改 Activity 的主题(here),但这也会影响 Activity 的其他菜单项
当菜单项出现在操作栏上时在它们之间放置分隔符的方法,但这里它们不会出现在工具栏本身上。它们出现在所选项目的弹出菜单上。
我尝试在列表和多余的item之间放假元素,我也尝试放一个组,一个空组,甚至尝试了各种属性。
遗憾的是没有任何效果。
问题
如何在操作项目的弹出菜单的特定项目之间添加分隔符?
也许我需要在单击操作项时创建一个自定义弹出菜单(如 here)?如果是这样,我如何在特定项目之间放置分隔符? 也许将 Spinner 用作操作项?
最佳答案
你应该使用 Action 布局
<menu 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"
tools:context=".LandingActivity">
<item
android:id="@+id/action_cart"
android:title="cart"
android:actionLayout="@layout/cart_update_count"
android:icon="@drawable/shape_notification"
app:showAsAction="always"/>
</menu>
然后 Action 布局可以有带分隔符的 TextView 。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/divider"/>
<TextView
android:id="@android:id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:gravity="center_vertical"
android:textAppearance="?attr/textAppearanceListItemSmall"/>
</LinearLayout>
然后你可以在代码中添加点击监听
关于android - 如何在特定菜单项之间添加分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33277124/