Android toolbar center title and custom font

前端 未结 30 2989
时光说笑
时光说笑 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:04

    Layout:

    
    
         
    
    

    Code:

        Toolbar mToolbar = parent.findViewById(R.id.toolbar_top);
        TextView mToolbarCustomTitle = parent.findViewById(R.id.toolbar_title);
    
        //setup width of custom title to match in parent toolbar
        mToolbar.postDelayed(new Runnable()
        {
            @Override
            public void run ()
            {
                int maxWidth = mToolbar.getWidth();
                int titleWidth = mToolbarCustomTitle.getWidth();
                int iconWidth = maxWidth - titleWidth;
    
                if (iconWidth > 0)
                {
                    //icons (drawer, menu) are on left and right side
                    int width = maxWidth - iconWidth * 2;
                    mToolbarCustomTitle.setMinimumWidth(width);
                    mToolbarCustomTitle.getLayoutParams().width = width;
                }
            }
        }, 0);
    

提交回复
热议问题