如果为AppTheme定义了默认字体,是否可以更改字体系列?

时间:2018-08-22 19:30:42

标签: android android-layout android-widget

我正在“应用程序”主题中为我的应用程序定义默认字体系列,例如:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="fontFamily">@font/trade_gothic_ltstd</item>
</style>

但是,对于某些小部件,我想要一个不同的字体系列。很正常吧?好的,设置android:fontFamily无效。它始终仍然使用默认的贸易哥特式字体。如果我从AppTheme中删除了fontFamily,那么我所有的fontFamily分配都会神奇地开始工作。

要实际上将字体更改为默认字体,我必须创建以下样式,并将其作为TextView应用于任何EditTextstyle=@style/KnockoutFont等。

<style name="KnockoutFont" parent="AppTheme">
    <item name="fontFamily">@font/knockout_htf49_liteweight</item>
</style>

这是预期的吗?

1 个答案:

答案 0 :(得分:1)

您所做的是正确的。但是,如果您正在寻找替代方法,则可以尝试以编程方式在代码中为所需的视图应用字体,如下所示:

//Setting typefaces - this will give stylish fonts
Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/FONT_NAME.EXTENSION");
textView.setTypeface(typeface);

现在,因为这是在程序中,所以它应该覆盖由主题设置的默认字体。我不确定,但是应该可以。如果您尝试过,请告诉我。

相关问题