android---Toast
Toast对象有两种获得方式,一种为显示非自定义对象时使用的,即可通过Toast.makeText(this, "长时间提示", Toast.LENGTH_LONG).show();使用;
当显示内容为自定义的View对象时,即通过实例化获得Toast对象
官方提示:只有使用custom view,setView(View)的时候,才使用new Toast(Content content)来得到Toast对象,否则必须用makeText(Context, int, int)方法来创建toast对象,并且这种方式获得Toast对象不能使用toast.setText(s)方法
代码
Toast tt=new Toast(ToastMain.this);
LayoutInflater inflater=this.getLayoutInflater();
final View view=inflater.inflate(R.layout.test, null);
tv=(TextView)view.findViewById(R.id.tv);
img=(ImageView)view.findViewById(R.id.img);
img.setImageResource(R.drawable.icon);
tv.setText("add a view for toast");
tt.setView(view);//添加自定义的View作为提示的内容
tt.setGravity(Gravity.BOTTOM, 0, 0);//设置Toast提示的位置xOffset:大于0向右移,小于0向左移
tt.setDuration(Toast.LENGTH_LONG);//设置显示时间
tt.show();


浙公网安备 33010602011771号