自定义吐司中的问题

时间:2016-03-11 05:33:41

标签: android

我正在创建一个自定义吐司,但它根本不起作用。

这是我的代码: -

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();
    }

2 个答案:

答案 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();
    }