1

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

@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

4

2 回答 2

5

由于您正在覆盖onFinishInflate,因此您似乎正在扩展View某种类型的 a 。

试试这个:

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

...如果这不起作用:

Typeface typeface = Typeface.createFromAsset(mTimeDisplay.getContext().getAssets(),
        "Fonts/Roboto-Light.ttf");
于 2012-10-01T19:34:23.557 回答
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()

于 2012-10-01T19:33:57.427 回答