2

我想在我的应用程序中自定义选项菜单。经过一些谷歌搜索并付出了很多努力,我仍然无法做到。这是我想要的:

在此处输入图像描述

menu.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
 <item
    android:id="@+id/menu_first"
    android:icon="@drawable/phone_selector"
    android:title="@string/first"/>

 <item
    android:id="@+id/menu_second"
    android:icon="@drawable/butterfly_selector"
    android:title="@string/second"/>

 <item
    android:id="@+id/menu_third"
    android:icon="@drawable/umbrella_selector"
    android:title="@string/third"/>
</menu>

OptionsMenuActivity.java:

public class OptionsMenuActivity extends Activity {

private Menu currentMenu;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.layout.menu, menu);
    this.currentMenu = menu;

    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    setMenuItemIconState(menu, MyApplication.getSelectedMenuItemDrawIcon(), false);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_phone:
        setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
        MyApplication.setSelectedMenuItemDrawIcon(R.drawable.phone_active);
        Intent intent = new Intent(OptionsMenuActivity.this, FirstActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        return true;

    case R.id.menu_butterfly:
        setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
        MyApplication.setSelectedMenuItemDrawIcon(R.drawable.butterfly_active);
        intent = new Intent(OptionsMenuActivity.this, SecondActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        return true;

    case R.id.menu_umbrella:
        setMenuItemIconState(currentMenu, MyApplication.getSelectedMenuItemDrawIcon(), true);
        MyApplication.setSelectedMenuItemDrawIcon(R.drawable.umbrella_active);
        intent = new Intent(OptionsMenuActivity.this, ThirdActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

private void setMenuItemIconState(Menu menu, int menuItemIconRes, boolean switchLastActiveMenuItemIcon) {
    int menuItemId = 0;
    int lastDeActiveDrawIcon = 0;
    switch (menuItemIconRes) {
        case R.drawable.phone_active:
            menuItemId = R.id.menu_first;
            lastDeActiveDrawIcon = R.drawable.phone;
            break;
        case R.drawable.butterfly_active:
            menuItemId = R.id.menu_second;
            lastDeActiveDrawIcon = R.drawable.butterfly;
            break;
        case R.drawable.umbrella_active:
            menuItemId = R.id.menu_third;
            lastDeActiveDrawIcon = R.drawable.umbrella;
            break;
    }
    if (switchLastActiveMenuItemIcon) {
        MenuItem menuItem = menu.findItem(menuItemId);
        menuItem.setIcon(lastDeActiveDrawIcon);
        return;
    }
    MenuItem menuItem = menu.findItem(menuItemId);
    menuItem.setIcon(MyApplication.getSelectedMenuItemDrawIcon());
 }
}

在 MyApplication.java 中,我只有一个 getter 和一个selectedMenuItemDrawIcon设置器。我设法正确设置了图标,但其他图标失败了。

是否可以像这样自定义选项菜单?如果没有,是否有任何其他方法可以创建 xml 文件并使其像 android 的基本菜单一样平滑地出现和消失。

任何帮助,将不胜感激。在此先感谢,(请提供代码示例)。

4

0 回答 0