findViewById在静态方法中

The*_*ter 8 android static-methods findviewbyid

我有这个静态方法:

public static void displayLevelUp(int level, Context context) {

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

    View layout = inflater.inflate(R.layout.custom_level_coast,
            (ViewGroup) findViewById(R.id.toast_layout_root));  // this row

    TextView text = (TextView) layout.findViewById(R.id.toastText);
    text.setText("This is a custom toast");

    Toast toast = new Toast(context);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();

    Toast.makeText(context, String.valueOf(level), Toast.LENGTH_SHORT)
            .show();

}
Run Code Online (Sandbox Code Playgroud)

但是,我无法弄清楚如何让第一个findViewById玩得很好,因为它说它是一种非静态方法.我理解为什么会这么说,但必须有一个解决方法?我context进入了这个方法,但无法一起完成它们.

tyc*_*czj 7

你可以做的一件事是使视图成为一个类范围的变量并使用它.我实际上并不建议这样做,但是如果你需要快速和肮脏的东西它会起作用.

作为参数传递视图将是首选方式