是否有可能在Android中使用花哨的字体?

Swe*_*per 0 fonts android xml-attribute

我知道TextView有一个属性被调用fontFamily,我认为你可以通过改变这个属性的值来改变文本的字体.所以我写道:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="my text"
    android:fontFamily="OCR A Std"
    android:textSize="20pt"/>
Run Code Online (Sandbox Code Playgroud)

然后我运行应用程序,但文本的字体仍然是普通的旧Arial字体.我认为必须有一种方法在android中使用花哨的字体,对吧?

如果我只是不能使用花哨的字体,什么字体(es)android支持?只有Arial是不可能的,对吧?

Ram*_*dal 7

你应该这样做

首先创建一个名为assets的文件夹并在其中创建另一个名为fonts的文件夹,现在将一些.ttf字体文件粘贴到其中,假设你的文件命名为neutra_bold.ttf

现在创建一个扩展TextView的类,例如.

package com.view9.costumviews;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class NeutraBoldTextView extends TextView {

    public NeutraBoldTextView(Context context) {
        super(context);
        Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/neutra_bold.ttf");
        this.setTypeface(face);
    }

    public NeutraBoldTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/neutra_bold.ttf");
        this.setTypeface(face);
    }

    public NeutraBoldTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/neutra_bold.ttf");
        this.setTypeface(face);
    }

    protected void onDraw (Canvas canvas) {
        super.onDraw(canvas);


    }

}
Run Code Online (Sandbox Code Playgroud)

现在要使用此视图,您可以像这样在xml布局中执行此操作

<com.view9.costumviews.NeutraBoldTextView

                android:id="@+id/tvViewAttachmentDescLabel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:ellipsize="end"
                android:maxLines="1"
                android:text="@string/text_description"
                android:textColor="@color/heading_text_color"
                 />
Run Code Online (Sandbox Code Playgroud)