如何使用android设置图标创建一个android菜单项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2999866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:13:35  来源:igfitidea点击:

How can I create an android menu item using android setting icon

android

提问by michael

Can you please tell me how can I create an android menu item using android setting icon?

你能告诉我如何使用android设置图标创建一个android菜单项吗?

回答by dbyrne

Here is a list of the standard icons. I don't see a "settings" icon. Perhaps you mean "Preferences" (ic_menu_preferences)?

这是标准图标列表。我没有看到“设置”图标。也许您的意思是“首选项”( ic_menu_preferences)?

You can set the icon programmatically like this:

您可以像这样以编程方式设置图标:

menu.add(0, MENU_QUIT, 0, "Quit").setIcon(R.drawable.menu_quit_icon);

You can also set it in your xml layout like this:

您还可以在您的 xml 布局中设置它,如下所示:

<item android:id="@+id/save_button"
      android:icon="@android:drawable/ic_menu_save"
      android:title="Save Image"/>

回答by Kristóf Dombi

You can see all the icons in the android SDK forder:

你可以在android SDK forder中看到所有的图标:

_your_install_path_\android-sdk\platforms\android-10\data\res\drawable-hdpi\

and then get a reference to them with:

然后通过以下方式获得对它们的引用:

android.R.drawable.ic_menu_preferences

just like it was your drawable.

就像它是你的 drawable 一样。

回答by Joshua Pinter

Add a new Vector Asset.

添加一个新的矢量资源。

  1. File -> New -> Vector Asset.
  1. 文件 -> 新建 -> 矢量资源。

enter image description here

在此处输入图片说明

  1. Click on the icon to change it.
  1. 单击图标进行更改。

enter image description here

在此处输入图片说明

  1. Select the icon you want (e.g. search for "setting").
  1. 选择您想要的图标(例如搜索“设置”)。

enter image description here

在此处输入图片说明

  1. Adjust other settings.

  2. Use that new Vector Asset in your xml.

    android:logo="@drawable/ic_settings_white_24dp"
    
  3. Party!

  1. 调整其他设置。

  2. 在您的 xml 中使用该新矢量资产。

    android:logo="@drawable/ic_settings_white_24dp"
    
  3. 聚会!

enter image description here

在此处输入图片说明

回答by Victor Ruiz.

If you want to handle the event, just try this on your activity

如果您想处理该事件,只需在您的活动中尝试此操作

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // action with ID action_refresh was selected
            case android.R.drawable.ic_popup_sync:
                Toast.makeText(this, "ic_popup_sync selected", Toast.LENGTH_SHORT)
                        .show();
                break;
            default:
                break;
        }

        return true;
    }

And in your menu folder use something like this:

在你的菜单文件夹中使用这样的东西:

<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="com.example.test.app.MainActivity"
    >

    <item android:id="@+id/action_settings1"
        android:icon="@drawable/abc_ic_search"
        android:title="Find Location"
        android:orderInCategory="100"
        app:showAsAction="ifRoom" />

    <item android:id="@+id/save_button"
        android:icon="@android:drawable/ic_menu_save"
        android:title="Save Image"/>

    <item android:id="@+id/refresh"
        android:icon="@android:drawable/ic_popup_sync"
        android:title="Refresh"/>


</menu>