菜单项不断增加,每按一次菜单按钮 [英] Menu items keeps on adding on every menu button press

查看:76
本文介绍了菜单项不断增加,每按一次菜单按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了两个菜单项.两者都很好用,但是每当我按下菜单按钮时,新的菜单项就会出现在旧菜单项旁边.如下所示,您可以看到

I have added two menu item . Both of them works good but whenever i press menu button new menu items appear beside the old one . Like below u can see

下面是我的菜单项xml

below is my menu item xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<item
android:title="Search"
android:icon="@drawable/search_white_24dp"
android:id="@+id/searchmenu"
app:showAsAction="always">

</item>
<item
android:icon="@drawable/settings_white_24dp"
android:title="Setting"
android:id="@+id/settingmenu"
app:showAsAction="always"
/>


</menu>

这是我的代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.searchmenu:
            editText = DuwaManager.openSearchBox(this,  
getSupportActionBar(), "DuwaListView");
            break;
        case R.id.settingmenu:

            break;

        case R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            break;
    }

请在我错的地方提供帮助?

please help where i am wrong ?

对不起,这个问题会持续很长时间,但是,我还有第二个问题

Sorry this will get to long but , I have second Question

2)我的第二个问题是我还有另一个菜单. xml中有共享图标代替搜索图标,但是当我尝试在具有相同代码菜单项的操作栏上显示时,不会在操作栏上显示.下面是我的另一个菜单xml

2) My second question i have another menu. xml in which there is share icon in place of search icon , But when i am trying to show on action bar with same code menu item does not show on action bar . below is my another menu xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:title="Share"
android:icon="@drawable/share_white"
android:id="@+id/share_tarika"
app:showAsAction="always"
/>
<item android:title="Setting"
    android:icon="@drawable/settings_white_24dp"
    android:id="@id/settingmenu"
    app:showAsAction="always"/>
</menu> 

及其代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.tarika_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.share_tarika:

            break;
        case R.id.settingmenu:

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

推荐答案

尝试一下

@Override
    public void onCreateOptionsMenu(Menu menu ) {
       menu.clear();// use menu.clear
       MenuInflater inflater = getMenuInflater();    
       inflater.inflate(R.menu."your current activity name ", menu);
    return true;
}

使用menu.clear()

use menu.clear()

这篇关于菜单项不断增加,每按一次菜单按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆