toolbar.getChildCount()返回0

时间:2016-02-26 15:00:06

标签: 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.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 个答案:

答案 0 :(得分:0)

您需要在getChildAt之后致电getChildCountsetSupportActionBar

相关问题