我能够使用此代码制作自定义吐司
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();
但是,由于我不明白 的目的LayoutInflater
,我将代码修改为...
Toast toast = new Toast(getApplicationContext());
toast.setView(findViewById(R.id.custom_toast));
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
我得到 RuntimeException 说“必须调用 setView”..
为什么我不能在不使用的情况下将视图分配给 toast
LayoutInflater
?一般的目的是什么,
LayoutInflater
以便我可以将此体验应用于其他自定义视图?
编辑:
我在onListItemClick()
接口方法中使用这些代码..设置内容后..