Android ActionBar常用功能代码整理

private ActionBar mActionBar;
mActionBar = this.getActionBar();
mActionBar.setTitle("");
mActionBar.setDisplayHomeAsUpEnabled(true);
/**
 *点击actionbar左侧home图标返回上一activity
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
/**
 *添加菜单
 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    super.onCreateOptionsMenu(menu);
    MenuItem clear = menu.add(0, 0, 0, "清除");
    clear.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
           //todo点击事件
            return false;
        }
    });
    clear.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    return true;
}

你可能感兴趣的:(Android ActionBar常用功能代码整理)