`

android -> 设置 全局 字体样式

 
阅读更多

 

首页在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"
                        />

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics