Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

android - How to display menu item with icon and text in AppCompatActivity

I tried different combinations in xml file:

<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">
    <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        app:showAsAction="ifRoom|withText" />
</menu>

or

<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">
    <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        app:showAsAction="always|withText" />
</menu>

or

<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">
    <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        app:showAsAction="withText" />
</menu>

or

<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">
    <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        android:showAsAction="always|withText" />
</menu>

I tried to set it programmaticly

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
        MenuItem item = menu.add(R.string.menu_create_alarm);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT|MenuItem.SHOW_AS_ACTION_IF_ROOM);
        item.setIcon(R.drawable.ic_action_accept);
        item.setOnMenuItemClickListener(
            new OnMenuItemClickListener(){

                @Override
                public boolean onMenuItemClick(MenuItem item){
                    saveAlarm();
                    return true;
                }
            }
        );


//      inflater.inflate(R.menu.menu_create_alarm, menu);
        super.onCreateOptionsMenu(menu, inflater);

    }

or

<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">
    <item
        android:id="@+id/action_create_alarm"
        android:icon="@drawable/ic_action_accept"
        android:orderInCategory="100"
        android:title="@string/menu_create_alarm"
        android:showAsAction="always|withText"
        app:showAsAction="always|withText" />
</menu>

However, Only Icon appears. And there is planty of room, cause I did not set toolbar title. Removing menues and replasing them with button inside toolbar is not sutable.
How to display text?

question from:https://stackoverflow.com/questions/32969172/how-to-display-menu-item-with-icon-and-text-in-appcompatactivity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use following code for establishing your purpose. No need to make it programmatic. You can trick in menu.xml file.

menu.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:id="@+id/overflowMenu"
    android:icon="@drawable/ic_more_vert_white_24dp"
    android:title=""
    app:showAsAction="always">

    <menu>
        <item
            android:id="@+id/menuWithIconText"
            android:icon="@drawable/chosen_icon"
            android:title="@string/menu_item_title"
            />
    </menu>
</item>

In above code, we've used menu inside of item. In inside menu, we've defined our overflow menu which we want with icon. Just remember to use app:showAsAction="always" in outside item tag.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...