菜单和选项菜单在一起

时间:2014-11-09 14:43:19

标签: android eclipse options-menu

如何在操作栏中创建主菜单以及在硬件按钮单击时打开的“选项”菜单?

像Whatsapp一样。有一个带有头像,名称和图标的动作栏,如果你按“菜单”按钮,它会出现另一个带有“显示联系人,媒体,搜索”等的人。

这是我的实际代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.calendar, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_legend) {
        // Open dialog
    } else if(id == R.id.action_settings) {
        startActivity(new Intent(this, SettingsActivity.class));
    } else if(id == R.id.action_add) {
        startActivity(new Intent(this, AddActivity.class));
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onKeyDown(int keycode, KeyEvent e) {
    switch(keycode) {
        case KeyEvent.KEYCODE_MENU:
            // Here open the options menu
            return true;
    }

    return super.onKeyDown(keycode, e);
}

0 个答案:

没有答案