选项菜单与选项之间的线。就像菜单中显示的选项之间的分隔线

Bha*_*uri 5 android menu

在我的应用程序中,我有一个选项菜单,它默认放置在顶部。当我们按下该菜单时,将在该菜单中打开它们的选项之间没有线。我想像分隔线一样显示它们之间的线。

我尝试更改主题,但我不会使用其他解决方案,因为如果我更改主题,它将完全影响我的应用程序。

它在 AppTheme 中工作,而不是在 AppTheme.AppCompat 中工作。

我希望它在主题没有变化的情况下工作。

如果有人想要代码,我会发布它。

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:showDividers">beginning</item>
    <item name="android:divider">@color/action_bar_divider</item>
</style>
Run Code Online (Sandbox Code Playgroud)

Vis*_*kar 4

有很多方法可以实现这一目标。

解决方案 1 ==> 您可以在菜单 xml 中进行分组,如下所示

<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=".MainActivity">

<group android:id="@+id/grp1">
<item
    android:id="@+id/navigation_item_1"
    android:checked="true"
    android:icon="@drawable/ic_home"
    android:title="@string/navigation_item_1" /></group>

<group android:id="@+id/grp2">
<item
    android:id="@+id/navigation_item_2"
    android:icon="@drawable/ic_home"
    android:title="@string/navigation_item_2" /></group></menu>
Run Code Online (Sandbox Code Playgroud)

解决方案 2==> 您可以制作自定义操作布局,如下所示

<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">
<item
    android:id="@+id/action_cart"
    android:title="cart"
    android:actionLayout="@layout/custom_layout"
    android:icon="@drawable/shape_notification"
    app:showAsAction="always"/>
Run Code Online (Sandbox Code Playgroud)

和 actionLayout 文件如下

<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"/>
Run Code Online (Sandbox Code Playgroud)

解决方案 3 ==> 您可以为特定活动制作自定义主题

<style name="CustomTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">@style/ActionBar</item></style>


 <style name="ActionBar"parent="android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@color/action_bar_background</item>
<item name="android:showDividers">beginning</item>
<item name="android:divider">@color/action_bar_divider</item></style>
Run Code Online (Sandbox Code Playgroud)

解决方案 4==> 您可以自定义 DropdownlistView 的视图

<style name="PopupMenuListView" parent="@style/Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#000000</item>
<item name="android:dividerHeight">1dp</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">   


 <item name="android:dropDownListViewStyle">@style/PopupMenuListView</item></style>
Run Code Online (Sandbox Code Playgroud)

您可以参考此链接在菜单项中添加分隔符