Android 如何从片段编辑操作栏菜单

我创建了一个使用片段的应用程序。从我的主活动中,我设置了操作栏。

但是在我的一个片段中,我需要修改动作图标并单击。

因此,使用下面的代码,当我加载我的片段时,它仍然显示MainActivity中的操作栏菜单。

这是我的主要活动:

public void restoreActionBar() {
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);

    // enable ActionBar app icon to behave as action to toggle nav drawer
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.main, menu);


        //Handle the Search Menu
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
        searchView.setQueryHint(this.getString(R.string.action_search));

        ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text))
        .setHintTextColor(getResources().getColor(R.color.white));      
        searchView.setOnQueryTextListener(OnQuerySearchView);

        mSearchCheck = false;


        restoreActionBar();
        return true;
    }

    return super.onCreateOptionsMenu(menu);
}//end onCreateOptionsMenu

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {

        /** CAMERA **/
        case R.id.action_camera:
            //openCamera();
            Utils.makeToast(getApplicationContext(), "Implement Camera", false);
            return true;

        /** SEARCH **/   
        case R.id.action_search:
            //openSearch();
            mSearchCheck = true;
            Utils.makeToast(getApplicationContext(), "Implement Search", false);
            return true;

        /** SETTINGS **/ 
        case R.id.action_settings:
            //openSettings();
            Utils.makeToast(getApplicationContext(), "Implement Settings", false);
            return true;

        /** ABOUT **/ 
        case R.id.action_help:
            //openHelp();
            Utils.makeToast(getApplicationContext(), "Implement Help", false);
            return true;


        default:
            return super.onOptionsItemSelected(item);
    }//end switch
}//end onOptionsItemSelected

private OnQueryTextListener OnQuerySearchView = new OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String arg0) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public boolean onQueryTextChange(String arg0) {
        // TODO Auto-generated method stub
        if (mSearchCheck){
            // implement your search here
        }
        return false;
    }
};//end OnQueryTextListener

以下是布局:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.sellbeesclean.MainActivity" >


    <!-- CAMERA -->
    <item
        android:id="@+id/action_camera"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_camera"
        android:title="@string/action_camera"
        app:showAsAction="ifRoom|collapseActionView"/>

    <!-- SEARCH -->
    <item
        android:id="@+id/action_search"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView"/>

    <!-- SETTINGS -->
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never"/>

    <!-- HELP -->
    <item
        android:id="@+id/action_help"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_help"
        android:title="@string/action_help"
        app:showAsAction="ifRoom|collapseActionView"/> </menu>

她是我的片段:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View rootView = inflater.inflate(R.layout.user_profile_fragment, container, false);
        Log.i(TAG, "onCreateView");
.....

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        setHasOptionsMenu(true);
    }

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

    }   



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {

            /** EDIT **/
            case R.id.action_edit:
                //openEditProfile(); //Open Edit Profile Fragment
                Utils.makeToast(MyApplication.getAppContext(), "Implement Camera", false);
                return true;



            default:
                return super.onOptionsItemSelected(item);
        }//end switch
    }//end onOptionsItemSelected

此处的片段菜单布局

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.sellbeesclean.MainActivity" >


    <!-- EDIT -->
    <item
        android:id="@+id/action_edit"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_edit"
        android:title="@string/action_edit_profile"
        app:showAsAction="ifRoom|collapseActionView"/></menu>

答案 1

在片段的 onCreateView 方法中写入

setHasOptionsMenu(true);

并在 onCreateOptionsMenu 方法中膨胀菜单 xml 文件

在 onCreateOptions 中,写入片段的菜单,写入

menu.clear();

在扩充菜单之前


答案 2

推荐