简体   繁体   中英

How to add a line between items in a menu application title bar in Android Studio?

I have a menu in the title bar of my Android app, that is not a pop-up Menu. In it I have some items. I want to add a line, or a separator, between just one pair of items in the list. I don't want dividers between all the items, just one pair. I tryed with groups who have different IDs, not worked, also tryed with android:actionlayout, no succes.

My current menu looks like this in design mode. I want to do something like this .

My XML containing my menu:

<?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>

My Java code for the menu:

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

Group your items in the XML menu, such as:

......

<group>
    <items...
</group>

<group>
    <items...
</group>

.....

And the in your code use:

final Menu menu = ((Toolbar)this.findViewById(R.id.your_toolbar)).getMenu();
MenuCompat.setGroupDividerEnabled(menu, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM