Android 有没有办法在溢出菜单项中添加图标?

Android 有没有办法在溢出菜单项中添加图标?,android,overflow-menu,Android,Overflow Menu,我正在使用基本导航抽屉中的溢出菜单 我想在菜单项上显示图标和文本 但不管我做什么都不行 这些是我在menu.xml代码中尝试的方法 首先,这是一种普通的方式来放置“安卓:图标” 溢出\u menu.xml <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"

我正在使用基本导航抽屉中的溢出菜单

我想在菜单项上显示图标和文本

但不管我做什么都不行

这些是我在menu.xml代码中尝试的方法

首先,这是一种普通的方式来放置“安卓:图标” 溢出\u 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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MovieList">
    <item
        android:id="@+id/menu_movie"
        android:orderInCategory="150"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="setting1"
        app:showAsAction="never" />
    <item
        android:id="@+id/menu_gallery"
        android:orderInCategory="150"
        android:title="setting2"
        android:icon="@drawable/ic_search_black_24dp"
        app:showAsAction="never" />

    <item
        android:id="@+id/settings3"
        android:orderInCategory="150"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="setting2"
        app:showAsAction="never" />
</menu>

第二,使用“android:actionLayout=”“”代码


溢出\u菜单\u title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_vertical"
    android:padding="2dp"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark">

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:src="@drawable/ic_15"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MOVIE"
        android:textSize="24dp"/>
</LinearLayout>

我尝试过任何方法,但都失败了

我不知道为什么我不能在里面放一个图标

请帮帮我


谢谢。

尝试将
弹出菜单
溢出菜单.xml一起使用

在“活动”中创建函数以初始化弹出窗口:

private void setupPopupMenu() 
{
    ImageButton menuButton = (ImageButton) findViewById(R.id.menu_button)

    PopupMenu popupMenu = new PopupMenu(this, menuButton);
    popupMenu.inflate(R.menu.overflow_menu);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_movie:
                    break;
                case R.id.menu_gallery:
                    break;
                case R.id.settings3:
                    break;
                default:
                    return false;
            }
            return true;
        }
    });
}
然后在
onCreate
方法中调用此函数

如果您正在使用Android主题中的默认
工具栏
或默认
操作栏
,您可能希望实现自定义
工具栏
布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center|start"
        android:text="Title"/>

    <ImageButton
        android:id="@+id/menu_button"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="?selectableItemBackgroundBorderless"
        android:src="<your-button-image>"/>

</LinearLayout>

尝试将
弹出菜单
溢出菜单.xml
一起使用

在“活动”中创建函数以初始化弹出窗口:

private void setupPopupMenu() 
{
    ImageButton menuButton = (ImageButton) findViewById(R.id.menu_button)

    PopupMenu popupMenu = new PopupMenu(this, menuButton);
    popupMenu.inflate(R.menu.overflow_menu);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_movie:
                    break;
                case R.id.menu_gallery:
                    break;
                case R.id.settings3:
                    break;
                default:
                    return false;
            }
            return true;
        }
    });
}
然后在
onCreate
方法中调用此函数

如果您正在使用Android主题中的默认
工具栏
或默认
操作栏
,您可能希望实现自定义
工具栏
布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center|start"
        android:text="Title"/>

    <ImageButton
        android:id="@+id/menu_button"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="?selectableItemBackgroundBorderless"
        android:src="<your-button-image>"/>

</LinearLayout>

您可以使用类在
onCreateOptionsMenu()
方法中以编程方式执行此操作,并设置
setOptionalConsVisible(true)


您可以使用类在
onCreateOptionsMenu()
方法中以编程方式执行此操作,并设置
setOptionalConsVisible(true)


谢谢你的回答。很抱歉,我正在使用Java。所以我不明白你的弹出代码..用Java代码更新了答案抱歉..我失败了..弹出菜单不显示..:(我使用的是Android提供的导航抽屉活动。这是原因吗?是的,在我的解决方案中,我假设您将使用带有父级
LinearLayout
和我建议的自定义工具栏的正常活动。我认为您无法在默认工具栏的弹出菜单中显示图标。谢谢您的回答。非常抱歉,我很抱歉我在使用Java。所以我不理解你的弹出代码..用Java代码更新了答案抱歉..我失败了..弹出菜单不显示..:(我使用的是Android提供的导航抽屉活动。这是原因吗?是的,在我的解决方案中,我假设您将使用一个带有父级
LinearLayout
和我建议的自定义工具栏的正常活动。我不认为您可以在默认工具栏的弹出菜单中显示图标。
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {

    inflater.inflate(R.menu.overflow_menu, menu);

    // Force showing icons for menu items
    if (menu instanceof MenuBuilder) 
        ((MenuBuilder) menu).setOptionalIconsVisible(true);
    
}