如何使用字体文件 [英] how to use font file

查看:214
本文介绍了如何使用字体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用自定义的字体文件。对于下面是我的code

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

<?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.

所以我必须把在一个地方,所有的类都可以使用it.No需要编写每个的TextView

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.

推荐答案

您可以创建一个自定义的TextView TextView的扩展设置自定义字体。

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:(在res /值)

<?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>

您会把Chantelli_Antiqua.ttf在资产的文件夹中。

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

编辑:看看这些问题使用自定义字体在Android的

<一个href=\"http://stackoverflow.com/questions/4395309/android-want-to-set-custom-fonts-for-whole-application-not-runtime\">Android:要设置自定义字体为整个应用程序不是运行

自定义字体和XML布局(Android版)

教程:

自定义XML属性为您定制的Andr​​oid窗口小部件

在Android 使用自定义字体

这篇关于如何使用字体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆