Android toolbar center title and custom font

前端 未结 30 3030
时光说笑
时光说笑 2020-11-22 04:11

I\'m trying to figure out the right way to use a custom font for the toolbar title, and center it in the toolbar (client requirement).

At the moment, i\'m using the

30条回答
  •  花落未央
    2020-11-22 05:13

    Solution that I used for this problem:

     public static void applyFontForToolbarTitle(Activity a){
            Toolbar toolbar = (Toolbar) a.findViewById(R.id.app_bar);
            for(int i = 0; i < toolbar.getChildCount(); i++){
                View view = toolbar.getChildAt(i);
                if(view instanceof TextView){
                    TextView tv = (TextView) view;
                    if(tv.getText().equals(a.getTitle())){
                        tv.setTypeface(getRuneTypefaceBold(a));
                        break;
                    }
                }
            }
        }
    

    For center gravity I think it would be necessary to change layout params to match_parent horizontally and then:

    tv.setGravity(Gravity.CENTER);
    

提交回复
热议问题