android Toast 重复显示问题
16lz
2021-01-26
我们知道,android,每次创建toast的时候,会放到系统的一个队列,然后依次显示,直到最后一个显示完全,这样有时候会导致,用户重复点击,导致toast显示很长时间,用户体验不好,如果只需要显示最新的toast,实现如下:
staticToastresult;publicstaticToastmakeText(inticon,Contextcontext,CharSequencetext){try{if(result!=null){result.cancel();result=null;}result=newToast(context);LayoutInflaterinflate=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);Viewv=inflate.inflate(R.layout.list_item_define_toast,null);TextViewtv=(TextView)v.findViewById(R.id.id_tv);tv.setText(text);ImageViewiv=(ImageView)v.findViewById(R.id.iv_iv_icon);iv.setImageResource(icon);result.setView(v);result.setDuration(0);result.setGravity(Gravity.TOP,0,0);returnresult;}catch(Exceptione){e.printStackTrace();}returnToast.makeText(context,text,0);}
自定义list_item_define_toast.xml布局文件代码:
<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@android:color/transparent"><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="60dp"android:background="@drawable/soild_black_rs_5_storke"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:orientation="horizontal"android:layout_marginLeft="15dp"android:layout_marginRight="15dp"><ImageViewandroid:layout_marginTop="15dp"android:layout_marginBottom="15dp"android:id="@+id/iv_iv_icon"android:layout_marginRight="6dp"android:layout_width="20dp"android:layout_height="20dp"android:layout_gravity="center_vertical"android:src="@drawable/refresh_success"/><TextViewandroid:layout_marginBottom="10dp"android:layout_marginTop="10dp"android:layout_gravity="center_vertical"android:id="@+id/id_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#ffffff"android:textSize="@dimen/jh_size_text_normal"android:text="我是一个Toast"/></LinearLayout></RelativeLayout></RelativeLayout>
更多相关文章
- android 点击EditText 弹出日期选择器DatePickerDialog
- android常用控件一二
- [原]Android(安卓)develop tricks——整理自国外的一些Blog
- TSwitch 中文简繁显示支持(XE6 Android)
- Android(安卓)沉浸式状态栏 最通俗易懂的总结
- 关于ScrollView嵌套RecyclerView时RecyclerView不显示的问题
- Android中WebView图片实现自适应的方法
- Android开发:实现popupwindow中显示listview
- Android开发中Activity切换导致的onCreate重复执行的问题