How to make android action bar title center aligned?

前端 未结 2 1724
借酒劲吻你
借酒劲吻你 2020-12-09 19:26

I tried my own Custom Action bar for getting action bar title centered on the middle. It worked. However, after I added the options menu in Action bar, the title again shift

2条回答
  •  庸人自扰
    2020-12-09 20:03

    First, create a .xml file like this: (action_bar.xml)

    
    
    
    
    
    
    

    And then, in your activity:

    View view = getLayoutInflater().inflate(R.layout.action_bar, null);
        ActionBar.LayoutParams params = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER);
    
        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Your Title");
    
    getSupportActionBar().setCustomView(view,params);       
    getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
    getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title
    

    I've tried a lot of solutions and only this one works. Hope it helps you.

提交回复
热议问题