Здесь я имею строку меню с двумя пунктами, называемых notification_icon
и favourite_icon
, когда я нажал favourite_icon
, которые должны быть изменены, как favourite_icon2
.so, что я попытался селектору. Селектор работает только для android:state_pressed="true"
не android:state_selected="true"
. s Пожалуйста, руководство мне сделать that.Here я добавил следующий код для refreenceКак изменить Menubar значок при нажатии на Android
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);
}
работает –
полный исходный код @ github https://github.com/hmkcode/Android/tree/master/android-menu-events-change –