我尝试使用自定义Toast 与ImageView
和TextView
。
当我触摸任何地方时,我希望我的Toast消失(点击按钮,触摸布局...),但它没有。
我读了Toast.class
文件并在调用新的Toast之前尝试使用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();
答案 0 :(得分:0)
解决方案:我使用<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@android:color/holo_red_dark" >
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/ripple"
android:state_pressed="true"/>
<item
android:drawable="@android:color/holo_red_dark"
android:state_selected="true"/>
<item android:drawable="@android:color/white"/>
</selector>
</item>
</ripple>
代替Toast.makeText(...)
,因为它会在隐藏的api(com.android.internal.R)中膨胀资源,而您无法访问它。使用new Toast(..)
初始化toast对象可以让您简单地访问隐藏的api。
Toast.makeText(...)