android定制敬酒

BLO*_*LOB 6 android toast

我能够使用此代码制作自定义吐司

    LayoutInflater inflater = getLayoutInflater();

    View layout = inflater.inflate(R.layout.custom_toast_layout, (ViewGroup)findViewById(R.id.custom_toast));

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

    Toast toast = new Toast(getApplicationContext());       
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();
Run Code Online (Sandbox Code Playgroud)

但是,由于我不明白其目的LayoutInflater,我将代码修改为...

Toast toast = new Toast(getApplicationContext());
    toast.setView(findViewById(R.id.custom_toast));
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.show();
Run Code Online (Sandbox Code Playgroud)

我得到RuntimeException,说"必须调用setView"..

  • 为什么我不能在不使用的情况下将视图分配给toast LayoutInflater

  • 通常的目的是什么LayoutInflater,以便我可以将此经验应用于其他自定义视图?

编辑: 我在onListItemClick()接口方法中使用这些代码..内容设置后..

Sou*_*Das 2

您的问题有您的答案,每个自定义视图都应该首先膨胀,这就是您修改后的代码出现错误的原因。