如何在Action Overflow中显示菜单项?

时间:2014-07-01 08:52:48

标签: android menu android-actionbar menuitem

我的经纪代码是:
 

    <item android:id="@+id/action_map"
        android:icon="@android:drawable/ic_dialog_map"
        android:title="Add"

        android:showAsAction="ifRoom"/>
    <item
        android:id="@+id/action_settings"
        android:showAsAction="never"
        android:title="@string/action_settings" />
</menu>

My Fragment代码是:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_where_am_i, container, false);
    setHasOptionsMenu(true);
    return root;
}
 @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    inflater.inflate(R.menu.where_i_am_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_settings:
            startActivity(new Intent(getActivity(), SettingsActivity.class));
            return true;
        case R.id.action_map:

            return true;
    }
    return super.onOptionsItemSelected(item);
}

我接下来的行动吧。为什么我在该列表中有2个设置?而且我不想添加标签&#34;添加&#34;到那个名单,我能做到吗? enter image description here

1 个答案:

答案 0 :(得分:1)

您应该清除菜单:

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

希望它可以帮助你:)