开发者

how to add image buttons in each Android activity?

can any one guide me how to开发者_运维知识库 add some menu related buttons at the bottom of each activity?


Do you mean the Options Menu?

how to add image buttons in each Android activity?

If so, you'll need to add some code like this to your Activity:

private static final int MENU_SEARCH = Menu.FIRST;
private static final int MENU_PREFERENCES = Menu.FIRST + 1;
private static final int MENU_HELP = Menu.FIRST + 2;

/* Creates the menu items */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, MENU_SEARCH, Menu.NONE, "Search")
            .setIcon(android.R.drawable.ic_menu_search);
    menu.add(Menu.NONE, MENU_PREFERENCES, Menu.NONE, "Preferences")
            .setIcon(android.R.drawable.ic_menu_preferences);
    menu.add(Menu.NONE, MENU_HELP, Menu.NONE, "Help")
            .setIcon(android.R.drawable.ic_menu_help);
    return true;
}

/* Handles item selections */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case MENU_SEARCH:
            search();
            return true;
        case MENU_PREFERENCES:
            preferences();
            return true;
        case MENU_HELP:
            showHelp();
            return true;
    }

    return super.onMenuItemSelected(featureId, item);
}

Note that Menu.add() returns the MenuItem created so you can chain the call to setIcon().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜