Android 带有操作栏 Sherlock 的菜单

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

Menu with action bar sherlock

androidmenuactionbarsherlock

提问by mkounal

I need an example or a tutorial on how to add menu items with action bar sherlock

我需要一个关于如何使用操作栏 Sherlock 添加菜单项的示例或教程

When I use the simple menu with the imports

当我使用带有导入的简单菜单时

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

when I call

当我打电话

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

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.goToSettings:
            startActivity(new Intent(this, SetPreference.class));
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }

    }

then I get the Cannot override the final method from SherlockActivity error.

然后我从 SherlockActivity 错误中得到了无法覆盖最终方法。

回答by StenaviN

You have to use Menu, MenuInflaterand MenuItemclasses from com.actionbarsherlock.viewpackage:

您必须使用Menu,MenuInflater和包中的MenuItemcom.actionbarsherlock.view

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

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

    return super.onCreateOptionsMenu(menu);
}

BTW, ActionBarSherlockcontains a lot of samples.

顺便说一句,ActionBarSherlock包含很多样本。

回答by Matt

I used @StenaviN 's answer above but ran into problems with onContextItemSelected. This postsolved it for me.

我在上面使用了 @StenaviN 的答案,但遇到了 onContextItemSelected 的问题。这篇文章为我解决了它。

Basically, you just have to use

基本上,你只需要使用

@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
    /* ... */
}

instead of

代替

@Override
public boolean onContextItemSelected(MenuItem item) {
    /* ... */
}

回答by Oliv

I used @Matt's answer above but ran into problems with onContextItemSelected.

我在上面使用了@Matt 的答案,但遇到了 onContextItemSelected 的问题。

Basically, you just have to use

基本上,你只需要使用

@Override
public boolean onContextItemSelected(com.actionbarsherlock.view.MenuItem item) {
    /* ... */
}

instead of

代替

@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
    /* ... */
}