This question already has an answer here:
这个问题在这里已有答案:
- Actionbar not shown with AppCompat 3 answers
- 操作栏未显示AppCompat 3的答案
I am following the tutorial on developer.android.com and trying to add items on action bar.
我正在关注developer.android.com上的教程并尝试在操作栏上添加项目。
Although i added all the code the search action shows as an overflow element instead of an action button element. I tried on 4" and 7" virtual devices with soft keyboard option.
虽然我添加了所有代码,但搜索操作显示为溢出元素而不是操作按钮元素。我尝试了带有软键盘选项的4“和7”虚拟设备。
Here is the
这里是
main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
Here is the MainActivity.java
's onCreateOptionsMenu
method.
这是MainActivity.java的onCreateOptionsMenu方法。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
I want to learn what causes this problem.
我想了解导致这个问题的原因。
2 个解决方案
#1
48
This is because if you use the support AppCompat ActionBar library and ActionBarActivity you should create your menus in a different than the standard way of creating xml menus in ActioBarSherlock or the default ActionBar.
这是因为如果您使用支持AppCompat ActionBar库和ActionBarActivity,您应该创建不同于在ActioBarSherlock或默认ActionBar中创建xml菜单的标准方式的菜单。
So try this code :
所以试试这个代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="always" />
<item android:id="@+id/action_compose"
android:icon="@drawable/ic_action_compose"
android:title="@string/action_compose"
app:showAsAction="always"/>
</menu>
#2
8
Though the issue is resolved, let me post an answer with more information, may be found helpful by anyone later.
虽然问题已经解决,但是让我发布一个包含更多信息的答案,以后可能会被任何人发现。
Now, the issue is you have used android:showAsAction="ifRoom"
and android:showAsAction="never"
, instead if you would want to make action button always visible then use android:showAsAction="always"
现在,问题是你使用了android:showAsAction =“ifRoom”和android:showAsAction =“never”,相反,如果你想让动作按钮始终可见,那么使用android:showAsAction =“always”
FYI, android:showAsAction can take either of any values:
仅供参考,android:showAsAction可以采用任何值:
android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
android:showAsAction = [“ifRoom”| “永远不会”| “withText”| “永远”| “collapseActionView”]
You can read more about Menu Resource
您可以阅读有关菜单资源的更多信息
#1
48
This is because if you use the support AppCompat ActionBar library and ActionBarActivity you should create your menus in a different than the standard way of creating xml menus in ActioBarSherlock or the default ActionBar.
这是因为如果您使用支持AppCompat ActionBar库和ActionBarActivity,您应该创建不同于在ActioBarSherlock或默认ActionBar中创建xml菜单的标准方式的菜单。
So try this code :
所以试试这个代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="always" />
<item android:id="@+id/action_compose"
android:icon="@drawable/ic_action_compose"
android:title="@string/action_compose"
app:showAsAction="always"/>
</menu>
#2
8
Though the issue is resolved, let me post an answer with more information, may be found helpful by anyone later.
虽然问题已经解决,但是让我发布一个包含更多信息的答案,以后可能会被任何人发现。
Now, the issue is you have used android:showAsAction="ifRoom"
and android:showAsAction="never"
, instead if you would want to make action button always visible then use android:showAsAction="always"
现在,问题是你使用了android:showAsAction =“ifRoom”和android:showAsAction =“never”,相反,如果你想让动作按钮始终可见,那么使用android:showAsAction =“always”
FYI, android:showAsAction can take either of any values:
仅供参考,android:showAsAction可以采用任何值:
android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
android:showAsAction = [“ifRoom”| “永远不会”| “withText”| “永远”| “collapseActionView”]
You can read more about Menu Resource
您可以阅读有关菜单资源的更多信息