Android中添加外部字体库和竖直排列字体

   一、在Android开发中会遇到系统提供的字体并不能满足自己对字体的设计需要,这就需要引进外部字体库了。下面简述一下如何引入外部字体库。

1>在自己工程文件目录下新建一个assets文件夹,在assets文件夹里面新建一个font文件夹。

具体路径如下图:

    2>然后将字体包如同图片文件一样放到font这个文件夹里面。

    3>利用TypeFace实现单个TextView的字体替换。

具体代码如下:

  1. tv_Text=(TextView)findViewById(R.id.tv_text);

  2. typeface=Typeface.createFromAsset(getAssets(),"font/main.ttf");

  3. tv_Text.setTypeface(typeface);

     4>有一种全局替换字体的方式如下:

         (1)、新建一个Application的类,类名自定义;

         (2)、在AndroidMainfest中声明这个类:

            如下代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mainexample">
 
    <application
        android:name=".FontApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".FontActivity" />
        <activity android:name=".circlemove"></activity>
    </application>
 
</manifest>

          android:name=".FontApplication"就是声明的这个类。

           (3)、在这个自定义的类中onCreate里面写下如下代码:

                    FontApplication.java

package com.example.mainexample;
 
import android.app.Application;
import android.content.Context;
import android.graphics.Typeface;
 
import java.lang.reflect.Field;
 
/**
 * Created by 尽途 on 2017/5/24.
 */
 
public class FontApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        setDefaultFont(this,"MONOSPACE","font/main.ttf");//更换字体的主要语句
    }
    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();
        }
    }
}
 

二、程序开发过程里面会遇到想让一句话竖直排列,这应该如何办呢?

       其实这个问题也很好解决。在程序中控制ems为1就可以了。

       1>在html中的解决方案:

 
  1. <TextView

  2. android:id="@+id/tv_text"

  3. android:text="你好"

  4. android:ems="1"

  5. android:textSize="56sp"

  6. android:layout_width="wrap_content"

  7. android:layout_height="wrap_content" />


      2>在java中的结局方案:

  1. tv_Text=(TextView)findViewById(R.id.tv_text);

  2. tv_Text.setMyEms(1);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专注VB编程开发20年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值