Android 设置字体的三种方法(TypeFace)

作者:Richard_tan0113 
来源:CSDN 
原文:https://blog.csdn.net/legend12300/article/details/69875816 

 

Android系统默认字体支持四种字体,分别为:

noraml (普通字体,系统默认使用的字体)
sans(非衬线字体)
serif (衬线字体)
monospace(等宽字体)
除此之外还可以使用其他字体文件(*.ttf)

一、使用系统自带的字体
1.在xml中修改字体

<!–  不指明typeface –>
            <TextView style=”@style/TextStyle”
                android:text=”Hello , world  中华人民共和国(没有设置属性)”/>
 
            <!–  使用默认的normal字体–>
            <TextView
                style=”@style/TextStyle”
                android:typeface=”normal”
                android:text=”Hello , world  中华人民共和国(xml设置normal)”/>
 
            <!–  使用默认的sans字体–>
            <TextView
                style=”@style/TextStyle”
                android:typeface=”sans”
                android:text=”Hello , world  中华人民共和国(xml设置sans)”/>
 
            <!–  使用默认的serifs字体–>
            <TextView
                style=”@style/TextStyle”
                android:typeface=”serif”
                android:text=”Hello , world  中华人民共和国(xml设置serif)”/>
 
            <!–  使用默认的monospace字体–>
            <TextView
                style=”@style/TextStyle”
                android:typeface=”monospace”
                android:text=”Hello , world  中华人民共和国(xml设置monospace)”/>

2.在Java代码中修改字体

        vSansText = (TextView) findViewById(R.id.sans);
        vSerifText = (TextView) findViewById(R.id.serif);
        vMonospaceText = (TextView) findViewById(R.id.monospace);
 
        //设置字体样式
        vSansText.setTypeface(Typeface.SANS_SERIF);
        vSerifText.setTypeface(Typeface.SERIF);
        vMonospaceText.setTypeface(Typeface.MONOSPACE);

二、在Android中可以引入其他字体

        //从asset 读取字体
        //得到AssetManager
        AssetManager mgr = getAssets();
        //根据路径得到Typeface
        Typeface tf = Typeface.createFromAsset(mgr, “fonts/HelveticaNeueLTPro-UltLt.otf”);
        //设置字体
        vTTFText.setTypeface(tf);
 

    原文作者:我的头上没有犄角
    原文地址: https://blog.csdn.net/qq_42250299/article/details/91363184
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞