添加项目到动作栏中(使用ActionBarSherlock)栏中、动作、项目、ActionBarSherlock

2023-09-12 03:19:41 作者:自己的屁股自己擦

我用ActionBarSherlock在我的项目,有时需要添加操作栏里面的一个或多个项目。

目前BaixadosFragment类(即扩展SherlockFragment),我使用的是下面的code和正常工作:

  @覆盖
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气)
{
    inflater.inflate(R.menu.main,菜单);
    super.onCreateOptionsMenu(菜单,充气);
}

公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
        案例R.id.refresh:
            刷新();
            返回true;
        默认:
            返回super.onOptionsItemSelected(项目);
    }
}
 

在这种情况下,我加入一个刷新按钮,女巫是寂寞内的main.xml

但我想在CupomDetalheActivity做同样的(虽然添加分享按钮),巫延伸SherlockFragmentActivity代替。所以,我不能够覆盖onCreateOptionsMenu,因为它有一个不同的签名(如下图):

  //这里面SherlockFragmentActivity
公众最终布尔onCreateOptionsMenu(android.view.Menu菜单){
    返回true;
}
//这里面SherlockFragment
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
    //没有在这里看到。
}
 
总结 底部动作栏设计

蒙山SherlockFragmentActivity,我甚至不知道我在哪里可以使用充气弹出包含分享按钮的XML ... 我AP preciate很多的任何意见和建议......

这个工作,根据DroidT的建议:

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
    MenuInflater充气= getSupportMenuInflater();
    inflater.inflate(R.menu.share,菜单);
    super.onCreateOptionsMenu(菜单);
    返回true;
}
 

解决方案

您SherlockFragmentActivity也有一个 onCreateOptionsMenu()在prepareOptionsMenu ()。您可以通过使用膨胀的 onCreateOptionsMenu()的菜单选项 getSupportMenuInflater()。你想打个电话给 invalidateOptionsMenu()在SherlockFragmentActivity,只要你想的变化发生在添加菜单选项上prepareOptionsMenu()。欲了解更多信息,看看本节链​​接。

I'm using ActionBarSherlock in my project and sometimes need to add one or more items inside the action bar.

At this BaixadosFragment class (that extends SherlockFragment), I'm using the following code and it works fine:

@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater)
{
    inflater.inflate(R.menu.main, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.refresh:
            refresh();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

In this case, I'm adding a refresh button, witch is lonely inside main.xml

BUT I want to do the same at CupomDetalheActivity (though adding a share button), witch extends SherlockFragmentActivity instead. So I'm not able to override "onCreateOptionsMenu" as it has a different signature (below):

//this is inside SherlockFragmentActivity
public final boolean onCreateOptionsMenu(android.view.Menu menu) {
    return true;
}
//this is inside SherlockFragment
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    //Nothing to see here.
}

Whith SherlockFragmentActivity, I don't even see where can I use the inflater to bring up the xml containing the share button... I appreciate a lot any ideas and suggestions...

[EDIT] This worked, according to DroidT's suggestion:

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

解决方案

Your SherlockFragmentActivity also has an onCreateOptionsMenu() and onPrepareOptionsMenu(). You can inflate your menu options in the onCreateOptionsMenu() by using getSupportMenuInflater(). You would want to make a call to invalidateOptionsMenu() in your SherlockFragmentActivity whenever you want the change to happen and add the menu options in onPrepareOptionsMenu(). For more information look at the "Changing menu items at runtime" section of this link.