Android更改字体 - getAssets()错误

时间:2012-10-01 19:30:29

标签: android

我的自定义字体有问题。我改变它:

@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
    Typeface typeface = Typeface.createFromAsset(getAssets(),
            "Fonts/Roboto-Light.ttf");
    mTimeDisplay.setTypeface(typeface);
    mAmPm = new AmPm(this);
    mCalendar = Calendar.getInstance();

    setDateFormat();
}

但我有错误:The method getAssets() is undefined for the type DigitalClock

2 个答案:

答案 0 :(得分:5)

由于你正在覆盖onFinishInflate,所以你似乎正在扩展某种View

试试这个:

Typeface typeface = Typeface.createFromAsset(getContext().getAssets(),
        "Fonts/Roboto-Light.ttf");

......如果不起作用:

Typeface typeface = Typeface.createFromAsset(mTimeDisplay.getContext().getAssets(),
        "Fonts/Roboto-Light.ttf");

答案 1 :(得分:2)

尝试使用context.getAssets()

创建字段:上下文上下文;

public class DigitalClock extends LinearLayout {

Context context;

public DigitalClock(Context context, AttributeSet attrs, int defStyle) 
{

    super(context, attrs, defStyle);
    this.context = context;

}

}

然后您将能够通过上下文访问getAssets()