[英]The menu item isn't showing in the ActionBar, only in overflow menu
I have exactly the same problem as in these threads: 我有与这些线程完全相同的问题:
No actions or icons showing on the actionbar; 操作栏上未显示任何操作或图标; only in overflow 只在溢出
Menu items won't show up in the actiobar 菜单项不会显示在活动栏上
No one has offered a solution, so I want to bring it up again. 没有人提供解决方案,因此我想再次提出。
I have a xml-file, my_menu.xml, that look like this: 我有一个xml文件my_menu.xml,看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/delete"
android:showAsAction="ifRoom|withText"
android:icon="@drawable/ic_delete_image"
android:title="delete"/>
</menu>
My activity extends ActionBarActivity and implements these methods: 我的活动扩展了ActionBarActivity并实现了以下方法:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.delete:
//delete something
break;
}
return(true);
}
In my book: "A value of ifRoom means that the menu item will appear in the action bar if there is space for it" - I have tested to use ifRoom, ifRoom|withText and always, but the item always ends up in the overflow menu. 在我的书中:“ ifRoom的值表示菜单项在有空间的情况下将显示在操作栏中”-我已经测试过始终使用ifRoom,ifRoom | withText,但是该项始终以溢出结束菜单。 I have tested the application on different devices, and it's plenty of room. 我已经在不同设备上测试了该应用程序,并且有足够的空间。 Do anyone have a solution to this? 有人对此有解决方案吗?
Hank 汉克
My activity extends ActionBarActivity 我的活动扩展了ActionBarActivity
As is covered in the documentation , if you are using the appcompat_v7
backport of the action bar, and its associated ActionBarActivity
, your menu resource needs to have showAsAction
in a namespace custom to your app: 如文档中所述 ,如果您正在使用操作栏的appcompat_v7
及其关联的ActionBarActivity
,则您的菜单资源需要在针对应用的自定义命名空间中具有showAsAction
:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/delete"
yourapp:showAsAction="ifRoom|withText"
android:icon="@drawable/ic_delete_image"
android:title="delete"/>
</menu>
Here is a complete sample project demonstrating the use of the appcompat_v7
action bar. 这是一个完整的示例项目,演示了appcompat_v7
操作栏的appcompat_v7
。
Try changing it to 尝试将其更改为
<item
android:id="@+id/delete"
android:showAsAction="always|withText"
android:icon="@drawable/ic_delete_image"
android:title="delete"/>
Hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.