我正在使用ActionBar。 我在ActionBar上有3个按钮,有3个活动。当我按下按钮时,它会导航到其他活动但我想要按下按钮我按下该按钮在ActionBar上显示它的选择状态。
我的菜单代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- Scan View -->
<item android:id="@+id/action_scan"
android:icon="@drawable/scan_icon_deselected_d1"
android:title="@string/scan_view"
android:showAsAction="always" />
<!-- Settings View -->
<item android:id="@+id/action_settings"
android:icon="@drawable/settings_icon_deselected_d1"
android:title="@string/settings"
android:showAsAction="always" />
<!-- Help Menu -->
<item android:id="@+id/action_help"
android:icon="@drawable/whitequestionmark_deselected"
android:title="@string/help_menu1"
android:showAsAction="always" />
在Style
<!-- Application theme. -->
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyTheme.ActionBar</item>
<item name="android:windowContentOverlay">@null</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="MyTheme.ActionBar" parent="android:Widget.Holo.ActionBar">
<item name="android:background">@android:color/transparent</item>
<item name="android:backgroundStacked">@android:color/transparent</item>
<item name="android:displayOptions">showTitle</item>
</style>
答案 0 :(得分:1)
试试这样:
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
for (int i = 0; i < mainMenu.size(); i++) {
// Set default icons
if (mainMenu.getItem(i).getItemId() == R.id.action1) {
mainMenu.getItem(i).setIcon(R.drawable.icon_default1);
} else if (mainMenu.getItem(i).getItemId() == R.id.action2) {
mainMenu.getItem(i).setIcon(R.drawable.icon_default2);
} else if (.....) {
.....
}
}
if (item.getItemId() == R.id.action1) {
item.setIcon(R.drawable.icon_highlighted1);
} else if (item.getItemId() == R.id.action2) {
item.setIcon(R.drawable.icon_highlighted2);
} else if(.....) {
.....
}
return super.onMenuItemSelected(featureId, item);
}
答案 1 :(得分:0)
我解决了它添加onCreateOptionsMenu(菜单菜单)。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
for (int i = 0; i < menu.size(); i++) {
if (menu.getItem(i).getItemId() == R.id.action_scan) {
menu.getItem(i).setIcon(R.drawable.scan_icon_deselected_d1);
} else if (menu.getItem(i).getItemId() == R.id.action_settings) {
menu.getItem(i).setIcon(R.drawable.settings_icon_deselected_d1);
} else if (menu.getItem(i).getItemId() == R.id.action_help) {
menu.getItem(i).setIcon(R.drawable.whitequestionmark_selected);
}
}
return super.onCreateOptionsMenu(menu);
}