您的位置:首页 > 移动开发 > Android开发

Android中ActionBar和ToolBar添加返回箭头的实例代码

2017-09-11 09:25 911 查看

 1.ActionBar添加返回箭头

//onCreate方法中
ActionBar actionBar = this.getSupportActionBar();
actionBar.setTitle("搜索功能");
actionBar.setDisplayHomeAsUpEnabled(true);
//activity类中的方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home)
{
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

在这里,ActionBar引入包:import android.support.v7.app.ActionBar;

2. ToolBar添加返回箭头

代码如下:

//onCreate函数中
Toolbar mToolbarTb = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbarTb);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//activity类中的方法
//添加点击返回箭头事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home)
{
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

总结

以上所述是小编给大家介绍的Android中ActionBar和ToolBar添加返回箭头的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息