Android——Toast

来源:互联网 发布:移动数据网络 编辑:程序博客网 时间:2024/06/03 14:02

一、Toast常用方法

(1)Toast.makeText(context,text,duration); //返回值为Toast

(2)toast.setDuration(duration); //设置持续时间

(3)toast.setGravity(gravity,xOffset,yOffset); //设置toast位置,gravity可为Gravity.XXXX

(4)toast.setText(s); //设置提示内容

(5)toast.show(); //显示

(6)相应的getXxxx方法

二、显示带图片的Toast

LinearLayout toast_layout=(LinearLayout)toast.getView(); //获取Toast的View布局,强转为LinearLayoutImageView iv=new ImageView(this);iv.setImageResource(R.drawable.toping);toast_layout.addView(iv,0);//0使图片位于toast中文字的上面toast.show();

三、自定义的Toast

1. 自己新建一个布局xml文件,如LinearLayout 布局xml文件,如命名为toast_layout.xml。

2.new一个Toast

LayoutInflater inflater=LayoutInflater.from(this);//将layout xml文件转为View的一种方法View toast_view=inflater.inflate(R.layout.toast_layout,null);Toast toast=new Toast(this);toast.setView(toast_view);toast.show();


0 0
原创粉丝点击