是否有可能使用花哨的字体在Android的? [英] Is it possible to use fancy fonts in Android?
问题描述
我知道的TextView
有称为属性 fontFamily中
,我还以为你可以改变文本的字体通过更改此属性的值。所以我写了:
I know that the TextView
has an attribute called fontFamily
and I thought you can change the font of the text by changing the value of this attribute. So I wrote:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="my text"
android:fontFamily="OCR A Std"
android:textSize="20pt"/>
然后我运行应用程序,但该文本的字体仍然是普通的老宋体般的字体。我认为有必须的是Android中使用花哨的字体的方式,对吧?
And then I run the app but the font of the text is still in the plain old Arial-like font. I think there must be a way to use fancy fonts in android, right?
如果我的不能的使用花哨的字体,什么字体(S)做(ES)Android的支持?只有宋体是不可能的,对吧?
If I just can't use fancy fonts, what font(s) do(es) android support? Only Arial is impossible, right?
推荐答案
你应该这样做这样
首先创建一个文件夹命名为资产,并创建另一个文件夹内它命名为字体现在粘贴一些的.ttf字体文件复制到它可以说你已经命名为neutra_bold.ttf文件
first create a folder named assets and create another folder within it named as fonts now paste some .ttf fonts file into it lets say you've file named as neutra_bold.ttf
现在创建一个扩展的TextView用于例如一类。
now create a class that extends TextView for eg.
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);
}
}
现在使用这个视图中,可以在XML布局这样做
now to use this view you can do this in xml layout like this
<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"
/>
这篇关于是否有可能使用花哨的字体在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!