Android 如何更改操作栏中的选项菜单图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26300480/
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
How to change option menu icon in the action bar?
提问by smartmouse
How to change the index icon of option menu?
如何更改选项菜单的索引图标?
I mean icon (3).
我的意思是图标 (3)。
Here is my code:
这是我的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
return true;
}
And here is the XML file:
这是 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" />
回答by Syed Raza Mehdi
The following lines should be updated in app -> main -> res -> values -> Styles.xml
应在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>
This is how it can be done. If you want to change the overflow icon in action bar
这是如何做到的。如果要更改操作栏中的溢出图标
回答by Parth Anjaria
I got a simpler solution which worked perfectly for me :
我得到了一个更简单的解决方案,它非常适合我:
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.change_pass);
toolbar.setOverflowIcon(drawable);
回答by Aditya varale
//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>
回答by Surendar D
1) Declare menu
in your class.
1)menu
在你的课堂上声明。
private Menu menu;
2) In onCreateOptionsMenu
do the following :
2)onCreateOptionsMenu
执行以下操作:
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_orders_screen, menu);
return true;
}
3) In onOptionsItemSelected
, get the item and do the changes as required(icon, text, colour, background)
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);
}
Note:
If you have 3 menu items :
menu.getItem(0) = 1 item,
menu.getItem(1) = 2 iteam,
menu.getItem(2) = 3 item
注意:
如果您有 3 个菜单项:
menu.getItem(0) = 1 item,
menu.getItem(1) = 2 itam,
menu.getItem(2) = 3 item
Based on this make the changes accordingly as per your requirement.
在此基础上,根据您的要求进行相应的更改。
回答by user7957410
This works properly in my case:
这在我的情况下正常工作:
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),
R.drawable.change_pass);
toolbar.setOverflowIcon(drawable);
回答by Joao Ferreira
Use the example of Syed Raza Mehdi and add on the Application theme
the name=actionOverflowButtonStyle
parameter for compatibility.
使用赛义德·拉扎迈赫迪的例子,并添加上Application theme
的name=actionOverflowButtonStyle
兼容性参数。
<!-- 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>
<!-- For compatibility -->
<item name="actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>
</style>
回答by Pravin
you can achieve this by doing
你可以通过这样做来实现这一点
<item
android:id="@+id/menus"
android:actionProviderClass="@android:style/Widget.Holo.ActionButton.Overflow"
android:icon="@drawable/your_icon"
android:showAsAction="always">
<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" />
</item>
回答by Luna Kong
Change your custom overflow icon of Actionbar in styles.xml
<resources> <!-- Base application theme. --> <style name=“MyTheme" parent="@android:style/Theme.Holo"> <!-- Pointer to Overflow style DONT forget! --> <item name="android:actionOverflowButtonStyle">@style/MyOverFlow</item> </style> <!-- Styles --> <style name="MyOverFlow" parent="@android:style/Widget.Holo.ActionButton.Overflow"> <item name="android:src">@drawable/ic_your_action_overflow</item> </style> </resources>
Put custom theme "MyTheme" in application tag of AndroidManifest.xml
<application android:name="com.example.android.MainApp" android:theme="@style/AppTheme"> </application>
在styles.xml 中更改Actionbar 的自定义溢出图标
<resources> <!-- Base application theme. --> <style name=“MyTheme" parent="@android:style/Theme.Holo"> <!-- Pointer to Overflow style DONT forget! --> <item name="android:actionOverflowButtonStyle">@style/MyOverFlow</item> </style> <!-- Styles --> <style name="MyOverFlow" parent="@android:style/Widget.Holo.ActionButton.Overflow"> <item name="android:src">@drawable/ic_your_action_overflow</item> </style> </resources>
将自定义主题“MyTheme”放在 AndroidManifest.xml 的应用程序标签中
<application android:name="com.example.android.MainApp" android:theme="@style/AppTheme"> </application>
Have fun.@.@
玩得开心。@。@
回答by Biplob Das
Check this out this may work: (in kotlin)
toolBar.menu.getItem(indexNumber).setIcon(R.drawable.ic_myIcon)
检查一下这可能有效:(在 kotlin 中)
toolBar.menu.getItem(indexNumber).setIcon(R.drawable.ic_myIcon)
回答by Hai Rom
If you want to change icon/title menu in the actionbar/toolbar programmatically,
如果要以编程方式更改操作栏/工具栏中的图标/标题菜单,
STEP by STEP
一步步
- Declare Menu in class
- 在课堂上声明菜单
private var mMenu: Menu? = null
私有变量 mMenu:菜单?= 空
Overide onCreateOptionsMenu method and find item so you need to change
override fun onCreateOptionsMenu(menu: Menu?): Boolean { mMenu = menu menuInflater.inflate(R.menu.menu, menu) if (mIsFavorite){ mMenu?.findItem(R.id.action_favorite)?.icon = yourDrawable } else { mMenu?.findItem(R.id.action_favorite)?.icon = yourDrawable } return true
}
Use invalidateOptionsMenu() to update some changes in menu after onCreateOptionsMenu executed. This method will re-create menu
覆盖 onCreateOptionsMenu 方法并找到项目,因此您需要更改
override fun onCreateOptionsMenu(menu: Menu?): Boolean { mMenu = menu menuInflater.inflate(R.menu.menu, menu) if (mIsFavorite){ mMenu?.findItem(R.id.action_favorite)?.icon = yourDrawable } else { mMenu?.findItem(R.id.action_favorite)?.icon = yourDrawable } return true
}
在 onCreateOptionsMenu 执行后,使用 invalidateOptionsMenu() 更新菜单中的一些更改。此方法将重新创建菜单