简体   繁体   English

android中的自定义字体

[英]Custom fonts in android

I am trying to use custom fonts in a textview: 我试图在textview中使用自定义字体:

tv=(TextView)findViewById(res);
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/font.ttf"); 
tv.setTypeface(font); 

But when I run I get the following error: 但是当我运行时,我收到以下错误:

W/System.err(  542): java.lang.RuntimeException: native typeface cannot be made

Whats the issue? 问题是什么?

For me, this definitely was the message I received when the font file couldn't be found. 对我来说,这肯定是我找不到字体文件时收到的消息。 Something as simple as: 简单的事情:

Typeface.createFromAsset(getContext().getAssets(), "fonts/MYFONT.TTF");

When my font was actually in font/MYFONT.TTF 当我的字体实际上是font / MYFONT.TTF

First of all check font's name and extension. 首先检查字体的名称和扩展名。 it is case sensitive & probably all caps. 它区分大小写并且可能全部大写。 eg. 例如。

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/ABADDON.TTF")

I would guess that there is a problem with the font itself. 我猜这个字体本身有问题。 That error will be triggered when native code in the OS attempts to load the typeface. 当操作系统中的本机代码尝试加载字体时,将触发该错误。 I seem to recall that there's a different message if the file is missing, so I think it is finding the file but not liking it for some reason. 我似乎记得,如果文件丢失,会有不同的消息,所以我认为它是找到文件但不喜欢它的原因。

I had encountered this problem i was setting the typeface inside a custom layout class with a constructor that pass a reference to parent activity's "context" and setting it up like this: 我遇到过这个问题我在自定义布局类中设置了字体,其构造函数传递了对父活动的“上下文”的引用并将其设置如下:

Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/font.ttf");

it gives me "native typeface cannot be made" error. 它给了我“原生字体无法制作”的错误。

went on creating a new test project out from scratch to just display the "Hello World, " with the custom font i want to use so i did this on onCreate() on the default activity class: 继续从头开始创建一个新的测试项目,只显示我想要使用的自定义字体的“Hello World”,所以我在默认活动类的onCreate()上做了这个:

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/font.ttf");

and this time it worked and i thought that maybe i should try putting a reference of the main Activity rather than the Context to my custom layout class: 这次它工作,我想也许我应该尝试将主Activity的引用而不是Context添加到我的自定义布局类:

Typeface font = Typeface.createFromAsset(activity.getAssets(), "fonts/font.ttf");

now this time it worked on the custom layout class. 现在这次它适用于自定义布局类。 hope this would help you guys too. 希望这也会帮助你们。

This might be the issue 这可能是个问题

Typeface.createFromAsset leaks asset stream : http://code.google.com/p/android/issues/detail?id=9904 Typeface.createFromAsset泄漏资产流: http//code.google.com/p/android/issues/detail? id = 9994

public class Harshida extends View {

Bitmap gBall;
float changingY;
Typeface font;
public Harshida(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    gBall=BitmapFactory.decodeResource(getResources(), R.drawable.greenball);
    changingY=0;
    font=Typeface.createFromAsset(context.getAssets(), "assets/G-Unit.TTF");
}
@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);

    Paint textPaint=new Paint();
    textPaint.setARGB(50,254,10,50);
    textPaint.setTextAlign(Align.CENTER);
    textPaint.setTextSize(50);
    textPaint.setTypeface(font);
    canvas.drawText("HarshidaParmar",canvas.getWidth()/2,200,textPaint);
    //canvas.drawBitmap(gBall,(canvas.getWidth()/2),0,null);
    canvas.drawBitmap(gBall,(canvas.getWidth()/2),changingY,null);
    if(changingY < canvas.getHeight()){
        changingY +=10;
    }else {
        changingY=0;
    }
    Rect middleRect= new Rect();
    middleRect.set(0, 40, canvas.getWidth(),400);
    //middleRect.set(0,0,0,0);
    Paint ourBlue = new Paint();
    ourBlue.setColor(Color.BLUE);
    canvas.drawRect(middleRect, ourBlue);
    //canvas.drawRect(middleRect,ourBlue);
    invalidate();

}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM