2016-02-26 101 views
3

我想改變我的應用程序標題的字體。爲了做到這一點,我決定遍歷工具欄,找到標題TextView,然後設置字體。toolbar.getChildCount()返回0

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    TextView title = null; 
    View child = null; 
    for(int i=0; i < toolbar.getChildCount(); i++){ 
     child = toolbar.getChildAt(i); 
     if(child instanceof TextView){ 
      title = (TextView) child; 
      break; 
     } 
    } 
    Typeface logoFont = Typeface.createFromAsset(getAssets(),"fonts/font.ttf"); 
    if(title!=null){ 
     title.setTypeface(logoFont); 
    } 
    setSupportActionBar(toolbar); 

但toolbar.getChildCount()返回0.工具欄似乎沒有任何子項。有人可以幫我弄這個嗎?

+1

在調用'getChildCount()'之前使您的工具欄可見。 – Berger

+0

感謝它的工作! – Surbhi

回答

0

您需要在setSupportActionBar之後致電getChildAtgetChildCount