Java getChildCount()返回0

Java getChildCount()返回0,java,android,android-actionbar,android-toolbar,viewgroup,Java,Android,Android Actionbar,Android Toolbar,Viewgroup,我正在尝试更改应用程序标题的字体。为了做到这一点,我决定遍历工具栏,找到标题TextView,然后设置字体 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); TextView title = null; View child = null; for(int i=0; i < toolbar.getChildCount(); i++){ child = toolbar.getChil

我正在尝试更改应用程序标题的字体。为了做到这一点,我决定遍历工具栏,找到标题TextView,然后设置字体

    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-Toolbar=(Toolbar)findviewbyd(R.id.Toolbar);
TextView title=null;
View child=null;
对于(int i=0;i

但是toolbar.getChildCount()返回0。工具栏似乎没有任何子项。有人能帮我吗?

setSupportActionBar
之后,您需要调用
getChildAt
getChildCount
,在调用
getChildCount()
之前使工具栏可见。