如何更改选项菜单的索引图标?
我的意思是图标(3)。
这是我的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
return true;
}
这是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" />
应在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>
这是如何做到的。如果要更改操作栏中的溢出图标
如果要以编程方式更改操作栏/工具栏中的图标/标题菜单,
一步步
private var mMenu:菜单? = null
这对我来说,只需像这样设置你的xml菜单:
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
我得到了一个更简单的解决方案,对我来说非常有效:
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.change_pass);
toolbar.setOverflowIcon(drawable);
1)在你的班级宣布menu
。
private Menu menu;
2)在onCreateOptionsMenu
做以下事项:
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_orders_screen, menu);
return true;
}
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);
}
注意: 如果您有3个菜单项: menu.getItem(0)= 1项, menu.getItem(1)= 2 iteam, menu.getItem(2)= 3项
在此基础上,根据您的要求进行相应的更改。
//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>
这适用于我的情况:
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),
R.drawable.change_pass);
toolbar.setOverflowIcon(drawable);
使用Syed Raza Mehdi的示例并在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>
你可以做到这一点
<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>
<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>
<application
android:name="com.example.android.MainApp"
android:theme="@style/AppTheme">
</application>
玩得开心。@。@
更改选项菜单索引图标颜色的简便方法是:
<!-- android:textColorSecondary is the color of the menu overflow icon (three vertical dots) -->
<item name="android:textColorSecondary">@color/optionMenuIconColor</item>
将上面的代码行添加到style.xml(自定义主题)文件中,希望你能得到答案,谢谢。