Android开发--更换字体

  • 开发中,经常遇到需要更换字体格式,设计妹子为了让UI更美,设置了很多让人着(tong)迷(ku)的字体,但是蓝瘦归蓝瘦,还是得撸起袖子开干。
  • Android系统中,默认提供三种字体:"sans", "serif", "monospace"

    如果设置字体为系统字体之一,在XML中,直接设置字体格式:
    1、sans

    <TextView
              Android:id="@+id/sans"
              Android:text="sans"
              Android:textSize="10sp"
              Android:typeface="sans" />

    2、monospace

    <TextView
              Android:id="@+id/monospace"
              Android:text="monospace"
              Android:textSize="10sp"
              Android:typeface="monospace" />

    3、serif

    <TextView
              Android:id="@+id/serif"
              Android:text="serif"
              Android:textSize="10sp"
              Android:typeface="serif" />
  • 使用自定义字体
    首先要引入自定义的字体文件,例如引入Roboto-Light字体,将Roboto-Light.ttf放入assets\fonts\目录下

引入字体文件.png


1、一般情况下,只针对部分字体,或者某个界面的少量字体进行修改
在XML布局中,不做任何修改:

  <TextView
            Android:id="@+id/textview "
            Android:text="custom"
            Android:textSize="10sp" />

在代码中:
得到TextView对象

TextView textView =(TextView)findViewById(R.id.textview);

创建Typeface对象

Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/Roboto-Light.ttf");

设置字体

textView.setTypeface(typeFace);

简单的三步,搞定。

2、上面是针对部分字体修改,但有时设计为了App的整体美观或者App产品本身的定位,绝大部分甚至所有字体,都需要使用自定义的字体。
如果再使用上面的方法逐一修改,对开发者来说无异于噩耗,而且,这种毫无意义的重复劳动,也不符合我们能懒就懒得程序员风格
那就想点省事的方法吧:
(1)、获取系统字体,并替换

public final class FontsOverride {

    public static void setDefaultFont(Context context,
            String staticTypefaceFieldName, String fontAssetName) {
        final Typeface regular = Typeface.createFromAsset(context.getAssets(),
                fontAssetName);
        replaceFont(staticTypefaceFieldName, regular);
    }

    protected static void replaceFont(String staticTypefaceFieldName,
            final Typeface newTypeface) {
        try {
            final Field staticField = Typeface.class
                    .getDeclaredField(staticTypefaceFieldName);
            staticField.setAccessible(true);
            staticField.set(null, newTypeface);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

在Application类中替换系统默认字体

public final class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FontsOverride.setDefaultFont(this, "DEFAULT", "Roboto-Light.ttf");
    }
}

(2)、上面是全局替换,但更多时候开发中只需要替换一部分,甚至有时想在XML中设置字体:
自定义CustomTextView对象

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

private void init() {
    if (!isInEditMode()) {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Light.ttf");
        setTypeface(tf);
    }
}

XML中使用自定义的CustomTextView

  <com.packagename.CustomTextView
                    android:id="@+id/custom"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Custom TextView"
                    android:textappearance="?android:attr/textAppearanceLarge"/>

总结:如果使用Android系统提供字体,直接在XML布局中设置TextView的typeface;
如果使用自定义字体,则根据具体需求,选择对应的方法



作者:T9的第三个三角
链接:http://www.jianshu.com/p/fc1a1ee0c189
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值