Android 使用 ActionBarSherlock 向 ActionBar 添加按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10278952/
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 03:07:42  来源:igfitidea点击:

Adding a button to the ActionBar with ActionBarSherlock

androidactionbarsherlockandroid-actionbar

提问by noloman

I have been trying to add a button to the SherlockActionBar but I can't get it working.

我一直在尝试向 SherlockActionBar 添加一个按钮,但我无法让它工作。

This is the code that I have:

这是我拥有的代码:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        android.view.MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, (android.view.Menu) menu);
        return super.onCreateOptionsMenu(menu);
    }

This is my menu.xmlcode:

这是我的menu.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/save_button"
          android:title="i"
          android:showAsAction="always" />
</menu>

This doesn't work, as even if I press the menu button, nothing shows up. Is there any other way? Am I making any mistake?

这不起作用,因为即使我按下菜单按钮,也没有任何显示。有没有其他办法?我犯了什么错误吗?

回答by hanspeide

You are using Android's Menu and MenuInflater, but should be using the classes that come with ActionBarSherlock:

您正在使用 Android 的 Menu 和 MenuInflater,但应该使用 ActionBarSherlock 附带的类:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
   inflater.inflate(R.menu.menu, (com.actionbarsherlock.view.Menu) menu);
   return super.onCreateOptionsMenu(menu);
}

It seems like you are intermingling the two right now. Make sure that you import only com.actionbarsherlock.view.Menu and com.actionbarsherlock.view.MenuInflater, and not its Android counterparts. I recommend you to do something like the following:

看来您现在正在将两者混在一起。确保您只导入 com.actionbarsherlock.view.Menu 和 com.actionbarsherlock.view.MenuInflater,而不是它的 Android 对应项。我建议您执行以下操作:

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;

...

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater inflater = getSupportMenuInflater();
       inflater.inflate(R.menu.menu, menu);
       return super.onCreateOptionsMenu(menu);
    }

回答by Joey

I think in the menu.xml. Your item does not declare android:showAsAction attribute completely. You must declare it like this:

我想在menu.xml 中。您的项目没有完全声明 android:showAsAction 属性。你必须像这样声明它:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/save_button"
          android:title="i"
          android:showAsAction="always|withText" />
</menu>

Since you did not specify any icon for the item action bar cannot display any item. By default icon are display than text.

由于您没有为项目操作栏指定任何图标,因此无法显示任何项目。默认情况下,图标显示而不是文本。