4

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

4

3 回答 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"/>
于 2013-07-25T14:09:25.400 回答
2

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

TextView mytextView=(TextView)findViewById(R.id.txtbx);
Typeface fonttype=Typeface.createFromAsset(getAssets(),"fonts/fontname.ttf");
mytextView.setTypeface(fonttype);
于 2013-07-25T14:09:31.637 回答
0

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

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>

于 2019-04-17T05:22:08.610 回答