如何设置相同的字体家族整个Android应用 [英] How to set same font family to entire android application

查看:104
本文介绍了如何设置相同的字体家族整个Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想设置相同的字体家族整个Android应用程序。我看到这个<一href="http://stackoverflow.com/questions/2711858/is-it-possible-to-set-font-for-entire-application">Is可以设置字体为整个应用程序?但使用code这一切都在做。我想设置在主题,这样它会在所有。它应转换的字体家族动作条文本也。

没有办法做到这一点?

styles.xml

 &LT;资源&GT;

    &LT;! - 
        基本应用的主题,依赖于API级别。这个主题被替换
        通过AppBaseTheme从RES /值-VXX /上较新的设备styles.xml。
     - &GT;
    &LT;样式名称=AppBaseTheme父=@风格/ Theme.AppCompat.Light&GT;
        &LT;! - 
            在新的API级别可以在可用主题自定义
            RES /值-VXX / styles.xml,而自定义相关
            向后兼容性可以去这里。
         - &GT;
    &LT; /风格&GT;

    &LT;! - 应用主题。 - &GT;
    &LT;样式名称=AppTheme父=AppBaseTheme&GT;
        &LT;! - 所有的定制,这不是针对特定的API级别的可以去这里。 - &GT;
    &LT; /风格&GT;

&LT; /资源&GT;
 

解决方案

如果你想设置字体为您的所有意见,而无需每次都做编程(仍然在所有版本的Andr​​oid的工作),你最好办法是子类的视图,并自动设置你想要的字体。

IE浏览器。

 公共类CustomTextView扩展TextView的{

    公共CustomTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        在里面();
    }

    公共CustomTextView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        在里面();
    }

    公共CustomTextView(上下文的背景下){
        超(上下文);
        在里面();
    }

    公共无效的init(布尔粗体){
        setTypeface(Typeface.createFromAsset(getAssets(),字体/ your_font_file.ttf));
    }
}
 

如果你想真正优化,即使进一步,可以使用静态引用,字体和使用,所以你不必每次都重新创建的字体查看负载。

Hello I want to set same font family to entire Android application. I have seen this Is it possible to set font for entire Application? but this all doing using code. I want to set in Theme such that it will work in all. It should convert the font family of ActionBar text also.

Any way to do this ?

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.

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.

这篇关于如何设置相同的字体家族整个Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆