How to implement android Toolbar Back button

后端 未结 6 977
野性不改
野性不改 2021-01-03 23:37

I am using a custom toolbar. I need to add back button to it. Now I am using this code to add the back button.

Toolbar toolbar = (Toolbar) getActivity().find         


        
6条回答
  •  抹茶落季
    2021-01-04 00:28

    Just add two new line of code. Something like this

    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
    toolbar.setBackgroundColor(getResources().getColor(R.color.white));
    toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back_arrow));
    
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
    

提交回复
热议问题