0

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

就像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);
}
4

0 回答 0