我的菜单项变得更大,因此我想将它们分组并制作一个分隔线来分隔每个组。 我现在该怎么办?
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--group1-->
<item
android:id="@+id/action_addtag"
android:title="@string/add_hashtag_string"
app:showAsAction="never" />
<item
android:id="@+id/action_block_list"
android:title="Block"
app:showAsAction="never" />
<item
android:id="@+id/action_report_list"
android:title="Report"
app:showAsAction="never" />
<!--group2-->
<item
android:id="@+id/terms"
android:title="Terms"
app:showAsAction="never" />
<item
android:id="@+id/feedback"
android:title="FeedBack"
app:showAsAction="never" />
<!--group3-->
<item
android:id="@+id/action_setting"
android:title="Setting"
app:showAsAction="never" />
</menu>
膨胀菜单时请务必致电
MenuCompat.setGroupDividerEnabled(menu, true);
,否则组将不会被分隔线分开!
示例:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_activity_main, menu);
MenuCompat.setGroupDividerEnabled(menu, true);
return true;
}
并确保您的菜单 xml 中有不同的组,例如:
<menu>
<group android:id="@+id/sorting" >
<item
android:id="@+id/action_sorting_new_old"
android:title="@string/action_sorting_new_old"/>
<item
android:id="@+id/action_sorting_a_z"
android:title="@string/action_sorting_a_z"/>
</group>
<group android:id="@+id/settings">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"/>
</group>
</menu>
唯一 ID 的组,我已经检查了实现,如果组具有不同的 id,它将创建一个分隔符。
示例菜单,创建分隔符:
<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 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/cart_update_count"
android:icon="@drawable/shape_notification"
app:showAsAction="always"/>
</menu>
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"/>
</LinearLayout>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> <!-- .Light.DarkActionBar"> -->
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">#17161B</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:dropDownListViewStyle">@style/PopupMenuListView</item>//<-add this
</style>
参考文献
<style name="PopupMenuListView" parent="@style/Widget.AppCompat.ListView.DropDown">
<item name="android:divider">#dddddd</item>
<item name="android:dividerHeight">1dp</item>
</style>
在同一个 res/values/styles.xml 文件中。希望有帮助!
假设您已添加新的菜单资源文件,请执行以下操作。
<group android:id="@+id/group1">
<item
android:id="@+id/action_about"
android:orderInCategory="100"
android:title="@string/about"
app:showAsAction="never" />
</group>
<group android:id="@+id/group2">
<item
android:id="@+id/action_settings"
android:orderInCategory="200"
android:title="@string/settings"
app:showAsAction="never" />
</group>
getMenuInflater().inflate(R.menu.main, menu);//inflate menu
MenuCompat.setGroupDividerEnabled(menu, true);//add horizontal divider
<group android:id="@+id/id_0">
...
</group>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
...
menu.setGroupDividerEnabled( true ); // this line
...
}
<item
android:title="@string/divider"
MetaJsoup:showAsAction="ifRoom|withText" />
<string name="divider">-------------</string>