android -> 设置 全局 字体样式

43 篇文章 0 订阅

 

首页在AndroidManifest.xml中 application中设置一个主题

<application
    android:name="com.qiyuan.congmingtou.app.CMTApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:persistent="true"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

  

 

在style.xml中

 

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:typeface">serif</item>
</style>

 

 

Android中默认的字体样式有3中,

serif,

monospace,

sans,

 

*** 如果是 全局加载 第三方 字体 ,需重写 控件 (比如TextView)

先将 下载的 TTF 字体文件 放入 assets/fonts/f3.ttf

 

然后 重写TextView 如  TextViewMy.java

package com.mft.test;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

/**
 * Created by tk on 2017/5/5 0005.
 */
public class TextViewMy extends TextView {
    public TextViewMy(Context context) {
        super(context);
        setTypeface() ;
    }
    public TextViewMy(Context context, AttributeSet attrs) {
        super(context, attrs);
        setTypeface() ;
    }
    public TextViewMy(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTypeface() ;
    }
    private void setTypeface(){
        // 如果自定义typeface初始化失败,就用原生的typeface
        try{
            setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/f3.ttf"));
        }catch(Exception e){
            Log.i("MyApp", "加载第三方字体失败。") ;
        }

    }

}

 

 

'然后在 布局文件中 就可以这样使用 了

                    <com.mft.test.TextViewMy
                        android:text="A"
                        android:id="@+id/key_a"
                        android:tag="a"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/btn_zh_abc_key"
                        android:layout_weight="1"
                        android:textColor="@color/keyb_zh_abc_color"
                        android:textSize="@dimen/zh_keyb_abc_font_size"
                        android:gravity="center"
                        />

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值