我的Android应用程序标题栏中有一个菜单,不是弹出式菜单。在其中我有一些项目。我想在列表中的一对项目之间添加一行或一个分隔符。我不想要所有项目之间的分隔线,只需要一对。我尝试了具有不同ID但没有工作的组,也尝试使用android:actionlayout,没有成功。
我的当前菜单在设计模式下看起来像this。我想做this之类的事情。
我的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 android:title="@string/editare_nume_jucatori">
<!-- submeniul meu -->
<menu>
<item
android:id="@+id/M_Jucator1"
android:enabled="true"
android:title="@string/Jucatorul1" />
<item
android:id="@+id/M_Jucator2"
android:enabled="true"
android:title="@string/Jucatorul2" />
</menu>
</item>
<item
android:id="@+id/M_Detalii"
android:icon="@drawable/dice10"
android:title="@string/detalii_text_meniu" />
<item
android:id="@+id/M_Despre_Aplicatie"
android:icon="@drawable/dice10"
android:title="@string/despre_aplicatie" />
<item
android:id="@+id/M_Iesire_Aplicatie"
android:icon="@drawable/m3"
android:title="@string/IesireAplicatie" />
</menu>
我的菜单Java代码:
Menu meniu1; //a variable used in my menu
//to show my menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.meniul_meu, menu);
meniu1 = menu; //this is my variable from up declaration
return true;
}
//here execute different actions for items clicked in menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
//click on my item ID from menu and execute
case R.id.M_Jucator1:
...(code code)...
return true;
//click on my item ID from menu and execute
case R.id.M_Jucator2:
..(code code)...
return true;
//click on my item ID from menu and execute
case R.id.M_Detalii:
..(code code)...
return true;
//cand dai click pe iesire din meniu
case R.id.M_Iesire_Aplicatie:
..(code code)..
return true;
default:
return super.onOptionsItemSelected(item);
}
} //finish meniu codes
答案 0 :(得分:0)
在XML菜单中将项目分组,例如:
......
<group>
<items...
</group>
<group>
<items...
</group>
.....
然后在您的代码中使用:
final Menu menu = ((Toolbar)this.findViewById(R.id.your_toolbar)).getMenu();
MenuCompat.setGroupDividerEnabled(menu, true);