更改Android App的默认字体

问题描述 投票:4回答:3

好的,我知道如何更改我的应用程序中单个文本框上使用的字体,但我希望我的应用程序中的所有文本都使用此自定义字体和颜色,目前我能想到的唯一方法是参考每个框并将它们设置为正确的字体和颜色。虽然它可行但似乎是一种笨重的方法,有更好的方法吗?

android fonts default true-type-fonts
3个回答
8
投票

您可以创建自定义TextView并在任何地方引用它。

public class TypefacedTextView extends TextView {

    public TypefacedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
        setTypeface(typeface);
    }
}

在view.xml里面

<packagename.TypefacedTextView
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="Hello"/>

2
投票

将您的不同字体文件放在assets文件夹中......

TextView mytextView=(TextView)findViewById(R.id.txtbx);
Typeface fonttype=Typeface.createFromAsset(getAssets(),"fonts/fontname.ttf");
mytextView.setTypeface(fonttype);

0
投票

通过在字体资源文件中创建Font-Family元素并将字体系列添加到应用主题

res>values>styles>

----------
<resources>
<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!--Add this below line-->
        <item name="android:fontFamily">@font/PacificoRegular</item>
    </style>
</resources>

© www.soinside.com 2019 - 2024. All rights reserved.