android字体格式设置解决方案

      前言:我们做APP的过程中字体有时不能达到我们的要求,那么这时候我们就要进行调整,其实也很简单,我们可以利用别人的ttf文件来获取我们想要的字体。
    首先我们想要获取ttf文件:http://pan.baidu.com/s/1c1E7wiO
    然后我们可以在APP中使用。
    新建assets文件,再在assets下建fonts文件,然后放置资源文件进去,如图:
这里写图片描述
下面直接可以使用了


        String fontsPath = "fonts" + File.separator;
        AssetManager assets = getAssets();
        typefaces[0] = Typeface.createFromAsset(assets, fontsPath + "one.ttf");
        typefaces[1] = Typeface.createFromAsset(assets, fontsPath + "two.ttf");
        typefaces[2] = Typeface.createFromAsset(assets, fontsPath + "three.ttf");
        typefaces[3] = Typeface.createFromAsset(assets, fontsPath + "four.ttf");
        typefaces[4] = Typeface.createFromAsset(assets, fontsPath + "five.ttf");
        typefaces[5] = Typeface.createFromAsset(assets, fontsPath + "six.ttf");
        typefaces[6] = Typeface.createFromAsset(assets, fontsPath + "seven.ttf");
        typefaces[7] = Typeface.createFromAsset(assets, fontsPath + "eight.ttf");
        typefaces[8] = Typeface.createFromAsset(assets, fontsPath + "nine.ttf");
        typefaces[9] = Typeface.createFromAsset(assets, fontsPath + "ten.ttf");
        txt1.setTypeface(typefaces[0]);
        txt2.setTypeface(typefaces[1]);
        txt3.setTypeface(typefaces[2]);
        txt4.setTypeface(typefaces[3]);
        txt5.setTypeface(typefaces[4]);
        txt6.setTypeface(typefaces[5]);
        txt7.setTypeface(typefaces[6]);
        txt8.setTypeface(typefaces[7]);
        txt9.setTypeface(typefaces[8]);
        txt10.setTypeface(typefaces[9]);

    大家打开可能有点慢,但是不要紧。
    这样就完成了对字体的设置。
    除了别人的字体,系统也有三种字体,分别是:

normal     bold   italic

    这几个属性我们一般可以直接在布局文件里面设置,如下:

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:gravity="center"
            android:textStyle="normal"
            android:text="系统normal"/>

    但是这样也有点麻烦,如果该页面每个控件都要这么弄不就比较繁琐了么,不要紧,我们还可以在style里面配置,分三种,如下:

    <style name="baseTextStyle2">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:fontFamily">sans-serif-light</item>
    </style>
    <style name="baseTextStyle3">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:fontFamily">sans-serif-condensed</item>
    </style>
    <style name="baseTextStyle4">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:fontFamily">sans-serif-thin</item>
    </style>

这里说一下:

android:fontFamily="sans-serif-light"
android:fontFamily="sans-serif-condensed"condensed
android:fontFamily="sans-serif-thin"(android 4.2)

ͬandroid:textStyle="normal|bold|italic"

是相互匹配的。
    最后给出源码:http://pan.baidu.com/s/1qYyQdHU

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值