自定义android中的选项菜单

时间:2012-12-12 06:23:52

标签: android menu customization options-menu

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

enter image description here

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 的setter。我设法正确设置了图标,但是其他人失败了。

是否可以自定义选项菜单?如果没有,是否还有其他方法可以创建一个xml文件,并使其像android的基本菜单一样顺畅地显示和消失。

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

0 个答案:

没有答案