1. 程式人生 > >Android 下不同格式字型的實現方法

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);
    }
}

字型效果圖