Android 菜单动态变化【添加或去除】

本文介绍了Android应用中菜单项的显示逻辑,包括如何根据不同设备方向和当前选中的标签来动态更新菜单项的可见性。此外,还展示了如何处理菜单项点击事件。
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/menu_items">
        <item android:id="@+id/menu_item_night_mode"
            android:title="@string/menu_item_night_mode"
            android:icon="@android:drawable/ic_menu_preferences"/>
        <item android:id="@+id/menu_item_settings"
            android:title="@string/menu_item_settings"
            android:icon="@android:drawable/ic_menu_preferences"/>
        <item android:id="@+id/menu_item_help"
            android:title="@string/menu_item_help"
            android:icon="@android:drawable/ic_menu_preferences"/>
    </group>
</menu>
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // We only want to show it as a menu in landscape, and only for clock/alarm fragment.
        mMenu = menu;
        //方向水平
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            if (mActionBar.getSelectedNavigationIndex() == ALARM_TAB_INDEX ||
                    mActionBar.getSelectedNavigationIndex() == CLOCK_TAB_INDEX) {
                // Clear the menu so that it doesn't get duplicate items in case onCreateOptionsMenu
                // was called multiple times.
                menu.clear();
                getMenuInflater().inflate(R.menu.desk_clock_menu, menu);


                MenuItem nightMode = menu.findItem(R.id.menu_item_night_mode);
                MenuItem help = menu.findItem(R.id.menu_item_help);
                if (mActionBar.getSelectedNavigationIndex() == ALARM_TAB_INDEX) {
                    nightMode.setVisible(false);
                    help.setVisible(false);
                } else if (mActionBar.getSelectedNavigationIndex() == CLOCK_TAB_INDEX) {
                    nightMode.setVisible(true);
                    help.setVisible(false);
                }

            }
            // Always return true for landscape, regardless of whether we've inflated the menu, so
            // that when we switch tabs this method will get called and we can inflate the menu.
            return true;
        }
        return false;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        updateMenu(menu);
        return true;
    }

    private void updateMenu(Menu menu) {
        // Hide "help" if we don't have a URI for it.
        MenuItem help = menu.findItem(R.id.menu_item_help);
        if (help != null) {
            Utils.prepareHelpMenuItem(this, help);
        }

        // Hide "lights out" for timer.
        MenuItem nightMode = menu.findItem(R.id.menu_item_night_mode);
        if (mActionBar.getSelectedNavigationIndex() == ALARM_TAB_INDEX) {
            nightMode.setVisible(false);
        } else if (mActionBar.getSelectedNavigationIndex() == CLOCK_TAB_INDEX) {
            nightMode.setVisible(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (processMenuClick(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private boolean processMenuClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_settings:
                startActivity(new Intent(DeskClock.this, SettingsActivity.class));
                return true;
            case R.id.menu_item_help:
                Intent i = item.getIntent();
                if (i != null) {
                    try {
                        startActivity(i);
                    } catch (ActivityNotFoundException e) {
                        // No activity found to match the intent - ignore
                    }
                }
                return true;
            case R.id.menu_item_night_mode:
                startActivity(new Intent(DeskClock.this, ScreensaverActivity.class));
            default:
                break;
        }
        return true;
    }

 

转载于:https://www.cnblogs.com/onelikeone/p/7582676.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值