Android menu item with both icon and text together when showAsAction is never

陌路散爱 提交于 2020-07-29 11:20:49

问题


Hi, how can I make my menu items have icons as well when showAsAction is never ?


回答1:


Use android:actionLayout in menu item tag to specify custom lyout (icon with text). Menu item will look like:

<item
    android:id="@+id/edit_menu"
    android:actionLayout="@layout/custom_edit_row"
    android:orderInCategory="100"
    android:title="@string/edit"
    app:showAsAction="always"></item>

Edit

custom_edit_row.xml

will be:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
style="@android:style/Widget.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true">

<ImageView
    android:id="@+id/imageViewEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/edit_ic" />

<TextView
    android:id="@+id/tvEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Edit" />




回答2:


<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=".YourActivityName">

<item
    android:id="@+id/action_settings"
    android:icon="@android:drawable/btn_dialog"
    android:title="@string/action_settings"
    app:showAsAction="always">

    <menu>
        <item
            android:id="@+id/profile"
            android:icon="@drawable/common_google_signin_btn_icon_light_disabled"
            android:title="PROFILE"
            app:showAsAction="always" />
    </menu>

</item>




回答3:


Use this menu file, it worked fine for me.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_m"
        android:showAsAction="never"
        android:title="menu">
        <menu>
            <item
                android:id="@+id/action_one"
                android:icon="@android:drawable/ic_popup_sync"
                android:showAsAction="always"
                android:title="Sync"/>
            <item
                android:id="@+id/action_two"
                android:icon="@android:drawable/ic_dialog_info"
                android:title="About"/>
        </menu>
    </item>
</menu>



回答4:


In createOptionsMenu, set the icon and text to MenuItem.

Elaborate the problem a little more.




回答5:


Just change root item of Harry Mad answer

    <item
    android:title="Ещё"
    app:showAsAction="always"
    android:icon="@drawable/ic_more_vert">
    ...


来源:https://stackoverflow.com/questions/33777318/android-menu-item-with-both-icon-and-text-together-when-showasaction-is-never

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!