问题描述
我要显示在工具栏(AppCompat-V7:22.1.1)一个溢出菜单,下面是我的menu_main.xml
I want to show a overflow menu in toolbar(AppCompat-v7:22.1.1), below is my menu_main.xml.
<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=".MainActivity">
<item
android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@mipmap/ic_menu_search"
android:orderInCategory="100"
android:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/menu_group_chat"
android:title="@string/menu_group_chat"
android:icon="@mipmap/ic_menu_groupchat" />
<item
android:id="@+id/menu_add_friend"
android:title="@string/menu_add_friend"
android:icon="@mipmap/ic_menu_add_friend" />
运行我的应用程序后,没有显示菜单项的图标,然后我想这solution,在我的Activty添加替代方法onMenuOpened()(范围是从AppCompatActivity),
After running my app, the icon of menu item is not displayed, then I tried this solution, add an override method onMenuOpened() in my Activty(extends from AppCompatActivity),
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if(menu!=null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try {
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return super.onMenuOpened(featureId, menu);
}
但在运行此演示后,我发现,仍然没有显示的图标。
But after running this demo, I find that the icon is still not displayed.
从这个报告的问题,我知道这AppCompatActivity.onMenuOpened不叫更多的22.x暂时,但奇怪的是,当我点击Genymotion硬件菜单键,菜单出现在底部,并带有图标,
From this reported issue, I know that AppCompatActivity.onMenuOpened is not called any more in 22.x, but it's odd that when I click the hardware menu key in Genymotion, the menu appear at the bottom and with the icon,
关闭菜单后,我再次点击工具栏上的溢出按钮,这些图标菜单显示,
after closing the menu, I click the overflow button in the toolbar again, these icons in menu appear,
这是多么奇怪!为什么出现这种情况?
how strange it is! Why this happens?
推荐答案
对于 AppCompactActivity你可以把这张支票上的在prepareOptionsPanel ()代替。
@Override
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
if (menu != null) {
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
try {
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "onMenuOpened...unable to set icons for overflow menu", e);
}
}
}
return super.onPrepareOptionsPanel(view, menu);
}
这篇关于在工具栏的溢出菜单请问这个奇怪的情况发生时,显示的菜单项的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!