Unable to show the 3dot menu

2019-09-02 16:04发布

问题:

I want to be able to show the free-dot menu always without using ActionBar. Just 3 dots menu + items in it, that's it. How can I do that? Here's my code

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MyMainActivity">
    <item
        android:id="@+id/empty"
        android:showAsAction="always"
        android:title="@string/dot_menu"
        android:icon="@android:drawable/ic_menu_more">

        <menu>
            <item
                android:id="@+id/item1"
                android:showAsAction="ifRoom"
                android:title="@string/item1" />

            <item
                android:id="@+id/item2"
                android:showAsAction="ifRoom"
                android:title="@string/item2" />

               <!-- ........... -->

And:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    //.....
}

It doesn't show a menu at all.

回答1:

Try this code, I hope it's helpful.

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/item_admin"
    android:showAsAction="ifRoom|withText"
    android:title="@string/item_admin"
    android:visible="false"
    tools:ignore="AppCompatResource" />

<item
    android:id="@+id/item_masterpass_preferences"
    android:showAsAction="ifRoom|withText"
    android:title="@string/text_1"
    android:visible="true"
    tools:ignore="AppCompatResource" />
<item
    android:id="@+id/item_shipping_addresses"
    android:showAsAction="ifRoom|withText"
    android:title="@string/text_2"
    android:visible="true"
    tools:ignore="AppCompatResource" />
<item
    android:id="@+id/item_switch_language"
    android:showAsAction="ifRoom|withText"
    android:title="@string/text_3"
    android:visible="false"
    tools:ignore="AppCompatResource" />
<item
    android:id="@+id/item_support_contact"
    android:showAsAction="ifRoom|withText"
    android:title="@string/text_4"
    android:visible="true"
    tools:ignore="AppCompatResource" />



标签: android menu