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

标签 android-actionbar android android-appcompat android-fonts

您好,我想为整个 Android 应用程序设置相同的字体系列。我看过这个Is it possible to set a custom font for entire of application?但这一切都在使用代码。我想在 Theme 中进行设置,使其在所有主题中都能正常工作。它还应该转换 ActionBar 文本的字体系列。

有什么办法吗?

样式.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>

最佳答案

如果您想为所有 View 设置字体而不必每次都以编程方式进行(并且仍然适用于所有 Android 版本),您最好的选择是将 View 子类化并让它自动设置字体你想要的。

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"));
    }
}

如果你真的想进一步优化它,你可以使用对该 TypeFace 的静态引用并使用它,这样你就不需要在每次加载 View 时都重新创建 TypeFace。

关于android-actionbar - 如何为整个Android应用程序设置相同的字体系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23135238/

相关文章:

android - 如何在操作栏中变回狭窄并改变颜色

java - 安卓 fragment 'requestFeature must be called'

java - HC-06蓝牙模块

android - AppCompactv21 工具栏作为 ActionBar 主题没有影响

android - 使用 Theme.AppCompat.Light.NoActionBar 时如何更改标题颜色

java - 无法理解 Fragments/Tabs + ActionBar + swipe (ICS)

android - 什么可以在 Android N+ 上加载私有(private)原生库?

android - 在 Lollipop 中清除 DNS 缓存的 Root Shell 命令?

java - 如何使用 eclipse 项目设置在 build.gradle 中声明 appcompat v7 依赖项?

Android:升级支持库后使用 findFragmentById() 出现 NullPointerException