源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-09-05 11:13 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android中ActionBar和ToolBar添加返回箭头的实例代码
[b] 1.ActionBar添加返回箭头[/b]
//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; [b]2. ToolBar添加返回箭头 [/b] 代码如下:
//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);
}
[b]总结[/b] 以上所述是小编给大家介绍的Android中ActionBar和ToolBar添加返回箭头的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程素材网网站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部