Android toolbar center title and custom font

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

    I don't know if anything changed in the appcompat library but it's fairly trivial, no need for reflection.

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    
    // loop through all toolbar children right after setting support 
    // action bar because the text view has no id assigned
    
    // also make sure that the activity has some title here
    // because calling setText() with an empty string actually
    // removes the text view from the toolbar
    
    TextView toolbarTitle = null;
    for (int i = 0; i < toolbar.getChildCount(); ++i) {
        View child = toolbar.getChildAt(i);
    
        // assuming that the title is the first instance of TextView
        // you can also check if the title string matches
        if (child instanceof TextView) {
            toolbarTitle = (TextView)child;
            break;
        }
    }
    

提交回复
热议问题