how to use font file

匆匆过客 提交于 2020-01-04 08:20:23

问题


I wanna use custom font file. For that below is my code

XML file:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/custom_font"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is the Chantelli Antiqua font." />
</LinearLayout> 

Java code:

   TextView txt = (TextView) findViewById(R.id.custom_font);  
   Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");  
   txt.setTypeface(font); 

Its working. But I want to use this custom fonts for whole project.

So i have to put in a place where all Class can use it.No need to write for every TextView.

Where and how can I use this custom font.


回答1:


You can make a custom TextView extend TextView to set custom fonts.

TextViewPlus.java:

package com.example;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class TextViewPlus extends TextView {
    private static final String TAG = "TextView";

    public TextViewPlus(Context context) {
        super(context);
    }

    public TextViewPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
        String customFont = a.getString(R.styleable.TextViewPlus_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    public boolean setCustomFont(Context ctx, String asset) {
        Typeface tf = null;
        try {
        tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
        } catch (Exception e) {
            Log.e(TAG, "Could not get typeface: "+e.getMessage());
            return false;
        }

        setTypeface(tf);  
        return true;
    }

}

attrs.xml: (in res/values)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewPlus">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:foo="http://schemas.android.com/apk/res/com.example"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.example.TextViewPlus
        android:id="@+id/textViewPlus1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:text="@string/showingOffTheNewTypeface"
        foo:customFont="Chantelli_Antiqua.ttf">
    </com.example.TextViewPlus>
</LinearLayout>

You would put "Chantelli_Antiqua.ttf" in the assets folder.

EDIT: Look at these Questions Using a custom typeface in Android

Android: Want to set custom fonts for whole application not runtime

Custom fonts and XML layouts (Android)

Tutorials:

Custom XML Attributes For Your Custom Android Widgets

Using Custom fonts on Android



来源:https://stackoverflow.com/questions/8504431/how-to-use-font-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!