how we can add menu item dynamically

后端 未结 5 640
清歌不尽
清歌不尽 2020-12-13 10:50

hi frnds am creating an application which is a tab application.

in my Home which extends sherlockFragmentActivity, i am inflating menu.xml and includes code for on o

5条回答
  •  忘掉有多难
    2020-12-13 11:25

    In the menu.xml you should add all the menu items. Then you can hide items that you don't want to see in the initial loading.

    
    

    Add setHasOptionsMenu(true) in the onCreate() method to invoke the menu items in your Fragment class.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
    

    You don't need to override onCreateOptionsMenu in your Fragment class again. Menu items can be changed (Add/remoev) by overriding onPrepareOptionsMenumethod available in Fragment.

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        menu.findItem(R.id.action_newItem).setVisible(true);
        super.onPrepareOptionsMenu(menu);
    
    }
    

提交回复
热议问题