【问题标题】:onCreateOptionsMenu inside Fragments片段内的 onCreateOptionsMenu
【发布时间】:2013-03-17 05:34:27
【问题描述】:

我已将setHasOptionsMenu(true) 放在onCreateView 中,但我仍然无法在片段中调用onCreateOptionsMenu

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                         Bundle savedInstanceState) {   
   setHasOptionsMenu(true);             
   return inflater.inflate(R.layout.facesheet, container, false);
}

下面是我的onCreateOptionsMenu 代码。

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

我得到的错误信息:

Fragment 类型的方法onCreateOptionsMenu(Menu) 必须覆盖或实现超类型方法。

【问题讨论】:

  • 感谢'setHasOptionsMenu(true);',我正在寻找那个。

标签: android android-fragments layout-inflater oncreateoptionsmenu


【解决方案1】:

试试这个,

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

最后,在onCreateView 方法中,添加此行以使选项出现在您的Toolbar

setHasOptionsMenu(true);

【讨论】:

  • 不加这行不会被调用:setHasOptionsMenu(true);
  • onCreateOptionsMenu() 的片段与活动的参数不同。
  • 和上面提到的活动中的 onCreateOptionsMenu() 布尔值不同,返回类型为 void。
  • 有趣的注意:如果你在你的包含的Activity中覆盖了onCreateOptionsMenu,两个选项菜单项都会被显示。
  • setHasOptionsMenu(true); 必须在 onCreate() 中调用才能完成。
【解决方案2】:

您已经拥有定义 action_settings 的自动生成文件 res/menu/menu.xml

在你的MainActivity.java中有以下方法:

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_settings:
            // do stuff, like showing settings fragment
            return true;
    }

    return super.onOptionsItemSelected(item); // important line
}

在 Fragment 调用的 onCreateView() 方法中:

setHasOptionsMenu(true); 

并添加这两种方法:

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_1:
            // do stuff
            return true;

        case R.id.action_2:
            // do more stuff
            return true;
    }

    return false;
}

最后,添加定义 action_1action_2 的新文件 res/menu/fragment_menu.xml

这样,当您的应用显示 Fragment 时,其菜单将包含 3 个条目:

  • action_1 来自 res/menu/fragment_menu.xml
  • action_2 来自 res/menu/fragment_menu.xml
  • action_settings 来自 res/menu/menu.xml

【讨论】:

  • 问题在片段中,而不是在活动中
  • @OlivierM 答案确实解释了片段菜单。我很欣赏它如何与活动菜单交互的解释。
  • 如何避免在片段选项中显示“action_settings”?
【解决方案3】:

我尝试了@Alexander Farber 和@Sino Raj 的答案。两个答案都很好,但我无法在片段中使用 onCreateOptionsMenu,直到我发现缺少的内容:

在我的 Activity 中添加 setSupportActionBar(toolbar),如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.id.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

我希望这个答案对遇到同样问题的人有所帮助。

【讨论】:

    【解决方案4】:

    打电话

    setSupportActionBar(toolbar)
    

    里面

    onViewCreated(...) 
    

    片段

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        ((MainActivity)getActivity()).setSupportActionBar(toolbar);
        setHasOptionsMenu(true);
    }
    

    【讨论】:

    • 谢谢你解决了我的问题
    【解决方案5】:

    Set setHasMenuOptions(true) 如果应用程序有一个带有 Actionbar 的主题,例如 Theme.MaterialComponents.DayNight.DarkActionBarActivity 有它自己的工具栏,否则片段中的 onCreateOptionsMenu 不会被调用。

    如果您想使用独立的Toolbar,您需要获取活动并将您的Toolbar 设置为支持操作栏

    (requireActivity() as? MainActivity)?.setSupportActionBar(toolbar)
    

    这让你的片段 onCreateOptionsMenu 被调用。

    另一种选择是,您可以使用toolbar.inflateMenu(R.menu.YOUR_MENU) 扩展您的Toolbar 自己的菜单,并使用

    toolbar.setOnMenuItemClickListener {
       // do something
       true
    }
    

    【讨论】:

      【解决方案6】:
       @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              View view = inflater.inflate(R.layout.activity_add_customer, container, false);
              setHasOptionsMenu(true);
      }
      
      @Override
      public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          inflater.inflate(R.menu.menu_sample, menu);
          super.onCreateOptionsMenu(menu,inflater);
      }
      

      【讨论】:

        【解决方案7】:

        您可以轻松做到, 在 Create 方法的片段中,

        setHasOptionsMenu(true)
        

        现在你可以覆盖了,

        override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
            super.onCreateOptionsMenu(menu, inflater)
            menu.clear()
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多