How to set the ActionBar title in middle instead of the default left aligned position?

前端 未结 5 957
予麋鹿
予麋鹿 2020-12-07 05:28

Trying to set the ActionBar title in middle instead of the default left aligned position. To do so, based on other answers this is what I\'ve done:



        
5条回答
  •  醉酒成梦
    2020-12-07 06:12

    You don't need a Toolbar for this. Just use a theme like Theme.AppCompat.Light.DarkActionBar with ActionBar in your activity and use the following code:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            getSupportActionBar().setCustomView(R.layout.actionbar_layout);
        }
        //your code
    }
    

提交回复
热议问题