How can I add items to my actionbar? [closed]

情到浓时终转凉″ 提交于 2019-12-25 06:36:55

问题


I've tried searching around but I am only limited to a very minimal amount of internet usage till 16th so I was looking around on how I could implement buttons to my action bar.

Right so when you enter the playlist section on my app, there is an about button on the layout. But id like to move that button to my action bar, how can I achieve this?


回答1:


This is an example how you can add HELP icon to your action bar

Create a menu XML inside your menu folder like the following

<item
    android:id="@+id/help_menu_item"
    android:icon="@android:drawable/ic_menu_help"
    android:title="Help"
    android:showAsAction="ifRoom" /> 

And in your activity do something like that

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.help_menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.help_menu_item:
            //do your menu press here       
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}



回答2:


    ActionBar mActionBar = getSupportActionBar();
    mActionBar.setDisplayShowTitleEnabled(false);
    mActionBar.setDisplayUseLogoEnabled(false);
    mActionBar.setDisplayHomeAsUpEnabled(false);
    mActionBar.setDisplayShowCustomEnabled(true);
    mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    mActionBar.setCustomView(R.layout.titlebar);

    homeButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.titlebar_iv_home);
    menuButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.titlebar_iv_menu);
    titlebar_title = (TitleTextView)mActionBar.getCustomView().findViewById(R.id.titlebar_title);
    titlebar_title.setText("TITLE");

you can give click events for the buttons also.Here i am taking one custom layout added to action bar. so create custom layout in your res/layout folder. and give appropriate id's to them.and give click events hope this helps



来源:https://stackoverflow.com/questions/23026050/how-can-i-add-items-to-my-actionbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!