我正在创建一个自定义吐司,但它根本不起作用。
这是我的代码: -
private void showCustomToast(String showToast) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(showToast);
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(inflater);
toast.show();
}
答案 0 :(得分:2)
您必须在setView()
方法中传递布局对象,而不是 inflater 。
答案 1 :(得分:1)
setView
部分的问题。通过View
对象。
<强>唐&#39;吨强>
toast.setView(inflater);
<强>不要强>
toast.setView(layout);
<强>最后强>
private void showCustomToast(String showToast) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(showToast);
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout); // Not inflater
toast.show();
}