【发布时间】:2015-05-11 07:54:31
【问题描述】:
我正在尝试使用带有ImageView 和TextView 的自定义吐司。
我希望我的 Toast 在我触摸任何地方(单击按钮、触摸布局...)时消失,但它不会。
在调用新的 Toast 之前,我阅读了 Toast.class 文件并尝试使用 cancel() 方法,但这并没有解决任何问题。谁能给我一个解决方案?
我的CustomToast.java:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View v = new View(context);
v = inflater.inflate(R.layout.custom_toast, (ViewGroup) v.findViewById(
R.id.layout_custom_toast));
layout = (RelativeLayout) v.findViewById(R.id.layout_custom_toast);
tvToast = (TextView) v.findViewById(R.id.tv_custom_toast);
tvToast.setText(text);
ivToast = (ImageView) v.findViewById(R.id.iv_custom_toast);
layout.setBackgroundResource(R.drawable.border_style_red);
ivToast.setBackgroundResource(R.drawable.warning);
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(v);
toast.show();
【问题讨论】:
-
你见过这个问题吗? stackoverflow.com/q/10070108/1979347
-
解决方案是基于(可解雇的)
AlertDialog构建一个Toast。很多关于它的教程和问题。 -
感谢您的回复。 @shkschneider - 我通过使用 Toast.makeText(...) 初始化 toast 对象、删除所有视图并添加 ImageView 和 TextView 解决了这个问题。
-
很高兴你解决了它。将您的解决方案发布为答案,然后自己接受以关闭此问题。
标签: android toast android-toast