TextView 加载第三方字体 ttf文件

1、简介

TextView 加载第三方字体文件

2、文件结构

在这里插入图片描述

3、xingkai.ttf

就是放在assets 资源文件下的第三方的 字体文件

4、activity_main.xml

就是布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="50dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text_one_id"
        android:text="这是自带的字体"
        android:textSize="30dp"
        android:typeface="sans"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:id="@+id/text_two_id"
        android:text="这是第三方字体"
        android:textSize="30dp"/>


</LinearLayout>

</LinearLayout>

5、Maniactivity 功能文件

package com.example.tssh.mytexttypeface;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    private TextView textViewOwn;
    private Typeface typeface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textViewOwn = (TextView) findViewById(R.id.text_two_id);
        typeface = Typeface.createFromAsset(getAssets(),"xingkai.ttf"); //加载第三方字体

        textViewOwn.setTypeface(typeface); //设置第三方字体

    }
}

6、现象展示

在这里插入图片描述

字体资源下载连接:

猜你喜欢

转载自blog.csdn.net/qq_27061049/article/details/86071696