How to align title at center of ActionBar in default theme(Theme.Holo.Light)

后端 未结 12 2162
野性不改
野性不改 2020-11-27 12:08

I searched a lot but can not find a way, How can I set then title at center of ActionBar instead of left aligned. I used below code to set the title at center :



        
12条回答
  •  爱一瞬间的悲伤
    2020-11-27 12:56

    It seems there is no way to do this without custom view. You can get the title view:

    View decor = getWindow().getDecorView();
    TextView title = (TextView) decor.findViewById(getResources().getIdentifier("action_bar_title", "id", "android"));
    

    But changing of gravity or layout_gravity doesn't have an effect. The problem in the ActionBarView, which layout its children by itself so changing of layout params of its children also doesn't have an effect. To see this excecute following code:

    ViewGroup actionBar = (ViewGroup) decor.findViewById(getResources().getIdentifier("action_bar", "id", "android"));
    View v = actionBar.getChildAt(0);
    ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    p.gravity= Gravity.CENTER;
    v.setLayoutParams(p);
    v.setBackgroundColor(Color.BLACK);
    

提交回复
热议问题