Android 菜單動態變化【添加或去除】

來源:互聯網
上載者:User

標籤:變化   sources   ted   bsp   efault   his   ring   help   menu   

<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;    }

 

Android 菜單動態變化【添加或去除】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.