如何在Android中按下时更改菜单栏图标

时间:2016-03-28 07:52:03

标签: android android-actionbar

我按下了notification_iconfavourite_icon两个项目的菜单栏,当我按下favourite_icon时应该更改为favourite_icon2。所以我尝试了选择器。选择器仅适用于android:state_pressed="true"而非android:state_selected="true"。 s请指导我这样做。我已经为您的refreence添加了以下代码

menu_clg.xml (under menu)

<?xml version="1.0" encoding="utf-8"?>
<menu

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appmunu="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".UserDashBoardFragment">
    <item
        android:id="@+id/action_notify"
        android:icon="@drawable/mail_icon"
        appmunu:showAsAction="always"
        android:title="Notification" />
    <item
        android:id="@+id/action_favourite"
        android:icon="@drawable/icon_selector"
        appmunu:showAsAction="always"
        android:title="Favourite" />


</menu>

这是icon_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:drawable="@drawable/vijay"

        />

    <item
        android:state_selected="false"
        android:drawable="@drawable/favourite_icon"
        />
</selector>

这是活动代码

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


        return true;
    }

    // delete the selected event from event list added here
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_notify:
                navigatetoNotification();
                return true;

            case R.id.action_favourite:
                favouriteClg();
                return true;


        }

        return super.onOptionsItemSelected(item);
    }

1 个答案:

答案 0 :(得分:2)

现在,我们想要做的是,当用户点击添加图标“+”时,我们将添加一个新项目并更改添加图标以删除“X”图标。

当用户点击某个项目时,将调用 onOptionsItemSelected()。 onOptionsItemSelected(MenuItem item)MenuItem对象作为参数传递,该参数是对所单击项的引用。使用此对象,我们可以知道已单击了哪个项目。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

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

boolean canAddItem = false;
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(item.getItemId() == R.id.action_addItem){
        // ( 1 ) add a new item 
            // ( 2 ) change add to remove
    }
    else{
            // if a the new item is clicked show "Toast" message.
    }

    return super.onOptionsItemSelected(item);
}

}

我们需要调用onPrepareOptionsMenu(菜单菜单)来添加,删除和修改菜单项。 在Android 2.3.x及更低版本中,每次用户打开选项菜单(按菜单按钮)时,系统都会调用onPrepareOptionsMenu()。 在Android 3.0及更高版本中,我们需要调用invalidateOptionsMenu()来请求系统调用onPrepareOptionsMenu()。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

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

boolean canAddItem = false;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Toast toast;
    if(item.getItemId() == R.id.action_addItem){
        invalidateOptionsMenu();
    }
    else{
        toast = Toast.makeText(this, item.getTitle()+" Clicked!", Toast.LENGTH_SHORT);
        toast.show();
    }

    return super.onOptionsItemSelected(item);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    if(canAddItem){
        menu.getItem(0).setIcon(R.drawable.ic_content_remove);
        MenuItem mi = menu.add("New Item");
        mi.setIcon(R.drawable.ic_location_web_site);
        mi.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        canAddItem = false;
    }
    else{
        menu.getItem(0).setIcon(R.drawable.ic_content_new);
        canAddItem = true;
    }

    return super.onPrepareOptionsMenu(menu);
}

}

希望它有所帮助。