如何更改操作栏中的选项菜单图标?

sma*_*use 49 android android-menu android-icons android-optionsmenu android-actionbar

如何更改选项菜单的索引图标?

在此输入图像描述

我的意思是图标(3).

这是我的代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options, menu);

    return true;
}
Run Code Online (Sandbox Code Playgroud)

这是XML文件:

<item android:id="@+id/Bugreport"
    android:title="@string/option_bugreport" />

<item android:id="@+id/Info"
    android:title="@string/option_info" />

<item android:id="@+id/About"
    android:title="@string/option_about" />
Run Code Online (Sandbox Code Playgroud)

Sye*_*hdi 63

应在app - > main - > res - > values - > Styles.xml中更新以下行

 <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
</style>

<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_launcher</item>
    <item name="android:background">?android:attr/actionBarItemBackground</item>
    <item name="android:contentDescription">"Lala"</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这是如何做到的.如果要更改操作栏中的溢出图标

  • 谢谢您,我很乐意为您提供帮助,实际上并不需要这样,但是我们在每张图片中都提供了描述,这就是我使用它的原因,而“ lala”在我的语言中表示好朋友;) (2认同)
  • 当使用[通过AppCompat的材料主题](https://chris.banes.me/2014/10/17/appcompat-v21/)时,这也有效:只需切换到`parent ="Widget.AppCompat.ActionButton.Overflow"`在MyActionButtonOverflow中. (2认同)

Par*_*ria 57

我得到了一个更简单的解决方案,对我来说非常有效:

Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.change_pass);
        toolbar.setOverflowIcon(drawable);
Run Code Online (Sandbox Code Playgroud)

  • 这种被低估的方法对我来说非常合适. (3认同)

Sur*_*r D 9

1)menu在你的班级宣布.

private Menu menu;
Run Code Online (Sandbox Code Playgroud)

2)onCreateOptionsMenu执行以下操作:

public boolean onCreateOptionsMenu(Menu menu) {
    this.menu = menu;
    getMenuInflater().inflate(R.menu.menu_orders_screen, menu);
    return true;
}   
Run Code Online (Sandbox Code Playgroud)

3)在onOptionsItemSelected,获取项目并根据需要进行更改(图标,文本,颜色,背景)

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_search) {
        return true;
    }
    if (id == R.id.ventor_status) {
        return true;
    }
    if (id == R.id.action_settings_online) {
        menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.history_converted));
        menu.getItem(1).setTitle("Online");
        return true;
    }
    if (id == R.id.action_settings_offline) {
        menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.cross));
        menu.getItem(1).setTitle("Offline");
        return true;
    }

    return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)

注意:
如果你有3个菜单项:
menu.getItem(0)= 1项,
menu.getItem(1)= 2 iteam,
menu.getItem(2)= 3项

在此基础上,根据您的要求进行相应的更改.


Adi*_*ale 8

//just edit menu.xml file    
//add icon for item which will change default setting icon
//add sub menus


 ///menu.xml file


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

            <menu>

                <item android:id="@+id/action_menu1"
                    android:icon="@android:drawable/ic_menu_preferences"
                    android:title="menu 1" />

                <item android:id="@+id/action_menu2"
                    android:icon="@android:drawable/ic_menu_help"
                    android:title="menu 2" />

            </menu>
        </item>
Run Code Online (Sandbox Code Playgroud)


小智 5

在我的情况下这正常工作:

Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),
                                              R.drawable.change_pass);
toolbar.setOverflowIcon(drawable);
Run Code Online (Sandbox Code Playgroud)