ActionBar的回退箭头设置

过于ActionBar的回退按钮有很多方法啦,这里我就讲一下我现在用方法,很简单很基础。

首先在onCreate()方法里面设置一下

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_password);
        //设置回退键
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
}

这样,那个返回的箭头就出来了 

但是现在点击是没有用的,还要重写一个方法

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

写完去试一下,可以回退了。 

本人也是刚刚接触到Android的知识,很多东西都是在积累中慢慢了解和深入的。写文章也算是对自己学习的一种交代!

猜你喜欢

转载自blog.csdn.net/qq_40480758/article/details/84101548
今日推荐