感谢大佬:https://blog.csdn.net/baidu_32682229/article/details/77927745
- 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);
}
补充:
https://blog.csdn.net/lovexieyuan520/article/details/9974929
https://blog.csdn.net/wsdssss/article/details/51556653