Android 使用AppCompat v7更改操作栏标题的字体和颜色

Android 使用AppCompat v7更改操作栏标题的字体和颜色,android,android-actionbar,android-appcompat,appcompatactivity,Android,Android Actionbar,Android Appcompat,Appcompatactivity,几个月前,我开发了一个android应用程序,该应用程序以API 19为目标,也使用API 19进行编译。我所做的一件事是,我根据滚动和其他因素动态更改了操作栏标题的字体和颜色。为此,我采用了以下方法: // change the font of action bar int titleId = getResources().getIdentifier("action_bar_title", "id", "android"); TextVi

几个月前,我开发了一个android应用程序,该应用程序以API 19为目标,也使用API 19进行编译。我所做的一件事是,我根据滚动和其他因素动态更改了操作栏标题的字体和颜色。为此,我采用了以下方法:

// change the font of action bar
        int titleId = getResources().getIdentifier("action_bar_title", "id",
            "android");
        TextView abTitle = (TextView) findViewById(titleId);
        Typeface typeFace = Typeface.createFromAsset(getAssets(),
                "fonts/Lato-Regular.ttf");
        abTitle.setTypeface(typeFace);


        //this part is dynamic based on the calculation of ratio 
        abTitle.setTextColor(getResources().getColor(R.color.white));
        abTitle.setAlpha(ratio);
现在我已切换到
材料设计
,并使用了
AppCompat v7:22+
。但是,当我运行应用程序时,它会崩溃,并在下面一行中给我一个
NullPointerException

abTitle.setTypeface(typeFace);
因此,它似乎获得了标题的id,但找不到
TextView


任何实现这一点的想法或其他方法都将不胜感激。

您可以查看此链接。您可以尝试此库,以避免使用setTypeFace。它使用的是android支持库v22.1,所以我认为在您的项目中不会有问题。但是,如何根据滚动因子动态更改颜色呢?以下是解决方案,我希望这将对您有所帮助。