2016-02-26 122 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