Android Toolbar Adding Menu Items for different fragments

后端 未结 10 1618
野性不改
野性不改 2020-11-28 22:51

I have a toolbar as well as a navigation drawer. When I start my app, the toolbar and navigation drawer are created. When I click items in the navigation drawer, it starts n

相关标签:
10条回答
  • 2020-11-28 23:40

    I solved this issues correctly. My solution is as following code:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mRootView = inflater.inflate(R.layout.fragment_order_history, container, false);
        //getActivity().invalidateOptionsMenu();
    
    
        setHasOptionsMenu(true);
    
        return mRootView;
    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.main, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
    
    0 讨论(0)
  • 2020-11-28 23:42

    Inside XML add this line

    app:menu="@menu/main_menu"
    
    0 讨论(0)
  • 2020-11-28 23:44

    Override onCreateOptionsMenu method in your every fragment.

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.your_menu_xml, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
    
    0 讨论(0)
  • 2020-11-28 23:47

    Add similar code to your fragments:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.library_fragment, parent, false);
        setHasOptionsMenu(true);
        return v;
    }
    
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
       inflater.inflate(R.menu.your_menu_xml, menu);
       super.onCreateOptionsMenu(menu, inflater);
    }
    

    This way you can customize the menu for your fragments.

    0 讨论(0)
提交回复
热议问题