一般我们提示的时候都是直接提示文字的,其实Toast也可以显示图片常用方法Toast.makeText(context,text,duration)这返回一个Toast对象toast.setDureation(duration)设置持续时间toast.setGravity(gra ...
一般我们提示的时候都是直接提示文字的,其实Toast也可以显示图片
常用方法
- Toast.makeText(context,text,duration)这返回一个Toast对象
- toast.setDureation(duration)设置持续时间
- toast.setGravity(gravity,xOffest,yOffset)设置Toast的位置
- toast.setText(s);设置内容
- toast.show()显示内容
- toast.setView(View v)
例子
1.只显示图片的Toast
public void showToast(){ //获取一个Toast对象,为下面操作准备 Toast toast = new Toast(this); ImageView img = new ImageView(this);
2.显示图片和文字
public void showToast2(){ Toast toast = Toast.makeText(this, "这是一个有图片的吐司", Toast.LENGTH_LONG); ImageView img = new ImageView(this); img.setImageResource(R.drawable.ic_launcher); //得到toast的布局对象 LinearLayout toast_layout = (LinearLayout) toast.getView(); //为toast添加图片资源,第二个参数,0表示图片在上 toast_layout.addView(img,1); toast.show();}
3.设计自己的Toast
有时候上面两种还没能满足自己的要求,就可以自定义布局(我在drawable中放了两张图片,詹姆斯和库里的)
准备布局文件
准备好你想要展示的Toast布局文件,我在layout文件夹新建了一个toast.
<?<LinearLayout "http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:layout_width="50dp" android:layout_height="90dp" android:background="@drawable/c2" /> <TextView android:layout_width="wrap_content" android:layout_height="90dp" android:gravity="center" android:text="VS" /> <ImageView android:layout_width="50dp" android:layout_height="90dp" android:background="@drawable/c1" /> </LinearLayout>
加载你的布局到Toast对象
public void showMyTosat(){ //把一个布局变成一个View对象 LayoutInflater inflater = LayoutInflater.from(this); View toast_layout = inflater.inflate(R.layout.toast, null); //设定 Toast toast = new Toast(this); toast.setView(toast_layout); toast.show();}
原标题:显示图片的(自定义)吐司Toast
关键词:图片
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。