2

頻繁に必要なすべてのメソッドをストックするために使用されるCommonCodeという名前のクラスがあります。

それらの方法の1つは、カスタムレイアウトでトーストを作成することです。そのトーストのTextViewにカスタムフォントを付けたいので、TypeFaceを使用します。アセットフォルダからカスタムフォントを取得しようとすると、うまくいきません。

「メソッドgetAsssets()がタイプContextに対して未定義です」という問題が発生します。

これが私のコードです:CommonCodeクラス

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(); 
}

}

みんなありがとう!

4

1 に答える 1

3

getAssets()の代わりに使用getAsssets(),

Typeface type= Typeface.createFromAsset(context.getAssets(), "fonts/aircruiser.ttf");  
text.setTypeface(type);
于 2012-04-08T08:35:11.710 に答える