Android 操作按钮未显示在操作栏上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20512644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:57:21  来源:igfitidea点击:

Action buttons doesn't show up on Action Bar?

android

提问by Salih Erikci

I am following the tutorial on developer.android.comand 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 onCreateOptionsMenumethod.

这是MainActivity.javaonCreateOptionsMenu方法。

 @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);
    }

enter image description here

在此处输入图片说明

I want to learn what causes this problem.

我想了解是什么导致了这个问题。

回答by Muthuraja

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>

回答by Paresh Mayani

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"and 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" | “总是” | “折叠动作视图”]

enter image description here

在此处输入图片说明

You can read more about Menu Resource

您可以阅读有关菜单资源的更多信息