Android 下不同格式字体的实现方法

从师兄那里看到了android 界面不同格式字体的实现方法,记录下来以便以后查看。

首先在Android Studio中建立assets文件夹,在assets文件夹下建立fonts文件夹,然后将字体文件.ttf文件拷贝至fonts文件夹下。


在android studio中建立assets文件夹的方法:在res文件夹傍边点击右键new-->Folder-->AssetsFolder即可;字体文件 .ttf 需要自己下载.实现不同格式的字体,下载对应的ttf文件就好,在windows中可在C:\Windows\Fonts目录下查找需要的.ttf文件.


来看一个不同格式字体的小例子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.chenhy.font.MainActivity">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="这是第一种字体!" />
    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="这是第二种字体!" />
    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="这是第三种字体!" />
</LinearLayout>

然后mainactivity代码

package com.example.chenhy.font;

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

public class MainActivity extends AppCompatActivity {

    TextView text1;
    TextView text2;
    TextView text3;

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

        text1 = (TextView)findViewById(R.id.text1);
        text2 = (TextView)findViewById(R.id.text2);
        text3 = (TextView)findViewById(R.id.text3);

        Typeface typeFace1 = Typeface.createFromAsset(getAssets(),"fonts/FZKTJT.ttf");
        Typeface typeFace2 = Typeface.createFromAsset(getAssets(),"fonts/FZQTJT.ttf");
        Typeface typeFace3 = Typeface.createFromAsset(getAssets(),"fonts/SJT.ttf");

        text1.setTypeface(typeFace1);
        text2.setTypeface(typeFace2);
        text3.setTypeface(typeFace3);
    }
}

字体效果图



猜你喜欢

转载自blog.csdn.net/chenhy24/article/details/54024440
今日推荐