我们知道,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>


更多相关文章

  1. android 点击EditText 弹出日期选择器DatePickerDialog
  2. android常用控件一二
  3. [原]Android(安卓)develop tricks——整理自国外的一些Blog
  4. TSwitch 中文简繁显示支持(XE6 Android)
  5. Android(安卓)沉浸式状态栏 最通俗易懂的总结
  6. 关于ScrollView嵌套RecyclerView时RecyclerView不显示的问题
  7. Android中WebView图片实现自适应的方法
  8. Android开发:实现popupwindow中显示listview
  9. Android开发中Activity切换导致的onCreate重复执行的问题

随机推荐

  1. Android(安卓)动画标签——translate
  2. Android音频开发(7):音乐可视化-FFT频谱图
  3. android和js的相互调用
  4. Android跨进程通信IPC之9——Binder之Fra
  5. Android中的适配器(Adapter)
  6. Android音频开发(2):使用AudioRecord录制pcm
  7. android:gravity / android:layout_Gravi
  8. xml字段含义
  9. 在程序中设置android:gravity 和 android
  10. mk中的android:sharedUserId和LOCAL_CERT