工具栏菜单选项未显示在fragmnet中

时间:2016-09-22 08:48:01

标签: android android-fragments

我正在使用“活动”工具栏中的菜单项对菜单进行充气,并覆盖片段中的选项。但是新菜单项未显示。

活动

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.encounter_tabacivity_menu, menu);
    return true;
}

片段

  @Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.clear();    //remove all items
    getActivity().getMenuInflater().inflate(R.menu.encounter_frag_problemlist_menu, menu);
}

@Override
public void onResume() {
    super.onResume();
    getActivity().invalidateOptionsMenu();
}
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        view = inflater.inflate(R.layout.fragment_problem_list, container, false);
        init();
        return view;
    }

4 个答案:

答案 0 :(得分:1)

您可能没有将toolbar设置为actionbar。要在actionbar中查看您的菜单,请将您的toolbar声明为actionbar,如下所示

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

答案 1 :(得分:0)

你的片段中需要setHasOptionMenu(true)。

<Nav className="navbar-abc-header navbar-default navbar-fixed-top">

答案 2 :(得分:0)

您必须在片段

中覆盖此内容
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    super.onCreateOptionsMenu(menu, inflater);
}

答案 3 :(得分:0)

在片段中尝试此代码:

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


@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_record ) {
//            startActivity(new Intent(getActivity(),RecordingActivity.class));
            startActivity(new Intent(getActivity(),RecordingTestActivity.class));
            getActivity().finish();
            return true;
        }
        if (id == R.id.action_search ) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }




@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.recording, menu);

        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        if (searchItem != null) {
            searchView = (SearchView) searchItem.getActionView();
        }
        if (searchView != null) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

            queryTextListener = new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextChange(String newText) {
                    Log.i("onQueryTextChange", newText);
                    return true;
                }
                @Override
                public boolean onQueryTextSubmit(String query) {
                    Log.i("onQueryTextSubmit", query);
                    return true;
                }
            };

            searchView.setOnQueryTextListener(queryTextListener);
        } super.onCreateOptionsMenu(menu, inflater);

    }