0

我正在尝试制作自定义吐司,但收到以下错误:

找不到符号 (ViewGroup) findViewById(R.id.toast_root));

同时findViewById()可以在其他代码行中看到。我的错误是什么?

public class MyCustomToast extends Toast {


public MyCustomToast(Context context){
    super(context);
}

public static Toast customToast(Context context, CharSequence text, int duration) {



    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_root));

    ImageView image = (ImageView) layout.findViewById(R.id.toast_image);
    image.setImageResource(R.drawable.skipper_toast);

    TextView text_toast = (TextView) layout.findViewById(R.id.toast_text);
    text_toast.setText(text);

    Toast toast = new Toast(context);

    toast.setGravity(Gravity.TOP, 0, 0);
    toast.setDuration(duration);
    toast.setView(layout);
    toast.show();

    return toast;
}
}
4

1 回答 1

1

以下是您应该如何更改代码:

public class MyCustomToast extends Toast {


public MyCustomToast(Context context){
    super(context);
}

public static Toast customToast(Activity activity, CharSequence text, int duration) {



    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) activity.findViewById(R.id.toast_root));

    ImageView image = (ImageView) layout.findViewById(R.id.toast_image);
    image.setImageResource(R.drawable.skipper_toast);

    TextView text_toast = (TextView) layout.findViewById(R.id.toast_text);
    text_toast.setText(text);

    Toast toast = new Toast(context);

    toast.setGravity(Gravity.TOP, 0, 0);
    toast.setDuration(duration);
    toast.setView(layout);
    toast.show();

    return toast;
}
}

希望这可以帮助。

于 2012-09-28T17:08:10.377 回答