普通类的Android getAssets给人"该方法getAsssets()是未定义的类型语境QUOT;错误 [英] Android getAssets in Common class gives "The method getAsssets() is undefined for the type Context" error
本文介绍了普通类的Android getAssets给人"该方法getAsssets()是未定义的类型语境QUOT;错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为通用code类,用于备货我经常需要的所有方法。
I have a class named CommonCode that is used for stocking all methods I frequently need.
其中的一个方法是创建一个自定义布局敬酒。我想给TextView的在祝酒自定义字体,所以我用字体。
当试图从我的资产文件夹获取自定义字体,它出错。
One of those methods is creating a toast with a custom layout. I want to give the TextView in that toast a custom font so I use a TypeFace. When trying to get the custom font from my assets folder, it goes wrong.
我得到的问题的方法getAsssets()是未定义的类型语境。
I get the issue "The method getAsssets() is undefined for the type Context".
下面是我的code:
通用code类
Here's my code: the CommonCode class
public class CommonCode {
public static void showToast(String toastString, Context context, View view) {
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) view);
ImageView image = (ImageView) layout.findViewById(R.id.toastImage);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.toastText);
Typeface type = Typeface.createFromFile(context.getAsssets(), "fonts/aircruiser.ttf");
text.setTypeface(type);
text.setText(toastString);
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}
}
谢谢你们!
推荐答案
使用 getAssets()
而不是 getAsssets()
:
Typeface type= Typeface.createFromAsset(context.getAssets(), "fonts/aircruiser.ttf");
text.setTypeface(type);
这篇关于普通类的Android getAssets给人"该方法getAsssets()是未定义的类型语境QUOT;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文