简体   繁体   English

如何为整个android应用程序设置相同的字体系列

[英]How to set same font family to entire android application

Hello I want to set same font family to entire Android application. 您好,我想为整个Android应用程序设置相同的字体系列。 I have seen this Is it possible to set a custom font for entire of application? 我已经看到了这是否可以为整个应用程序设置自定义字体? but this all doing using code. 但这一切都使用代码完成。 I want to set in Theme such that it will work in all. 我想在Theme进行设置,使其可以全部使用。 It should convert the font family of ActionBar text also. 它也应该转换ActionBar文本的字体系列。

Any way to do this ? 有什么办法吗?

styles.xml styles.xml

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

If you want to set the TypeFace for all of your Views without having to do it programatically each time (and still work across all versions of Android), your best option would be to subclass the View and have it automatically set the TypeFace you want. 如果您想为所有视图设置TypeFace而不必每次都以编程方式进行设置(并且仍然可以在所有版本的Android上使用),则最好的选择是对View进行子类化,并使其自动设置所需的TypeFace。

IE. IE浏览器。

public class CustomTextView extends TextView{

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomTextView(Context context) {
        super(context);
        init();
    }

    public void init(boolean bold) {
        setTypeface(Typeface.createFromAsset(getAssets(), "fonts/your_font_file.ttf"));
    }
}

If you want to really optimize that even further, you can use a static reference to that TypeFace and use that so you don't need to recreate the TypeFace every time the View loads. 如果您真的想进一步优化它,可以使用对该TypeFace的静态引用并使用它,这样就不必在每次加载View时都重新创建TypeFace。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM