Toolbar title not in center when Back Button is enable

前端 未结 6 678
名媛妹妹
名媛妹妹 2021-02-02 13:53

I\'m trying to display my toolbar title in the center and to do it I use the method which is given in this answer :-Toolbar Center title

However, when I enable back butt

6条回答
  •  你的背包
    2021-02-02 14:09

    Dont set propterties like this

    mActionBar = getSupportActionBar();
    mActionBar.setDisplayHomeAsUpEnabled(true);
    mActionBar.setDisplayShowHomeEnabled(true);
    

    Do like this

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            // Title and subtitle
            toolbar.setTitle(R.string.about_toolbar_title);
            toolbar.setSubtitleTextColor(Color.WHITE);
            toolbar.setTitleTextColor(Color.WHITE);
            toolbar.setBackgroundColor(getResources().getColor(
                    R.color.themeToolbarColor));
            toolbar.setNavigationIcon(R.drawable.ic_action_back);
            toolbar.setNavigationOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                   finish();  // to go back  finish() will do your work.
                    //mActionBar.setDisplayHomeAsUpEnabled(true);
                    //mActionBar.setDisplayShowHomeEnabled(true);
                }
            });
    

提交回复
热议问题