操作按钮不能在操作栏显示出来? [英] Action buttons doesn't show up on Action Bar?
问题描述
我下面的 developer.android.com 的教程,并尝试添加的操作栏项目。
I am following the tutorial on developer.android.com and trying to add items on action bar.
虽然我添加了所有code中的搜索行为显示为溢出的元素,而不是一个操作按钮元素。我尝试了4和7个软键盘选项的虚拟设备。
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.
下面是
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>
下面是 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.
推荐答案
这是因为如果使用支持AppCompat动作条图书馆,ActionBarActivity你应该比ActioBarSherlock或创建XML的菜单的标准方法不同的创建你的菜单默认的动作条。
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.
那么试试这个code:
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>
这篇关于操作按钮不能在操作栏显示出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!