1.
Toast相关属性
Toast toast = Toast.makeText(mContext, "我是Toast", Toast.LENGTH_SHORT);
/**
* gravity:布局
* xOffset:相对于当前位置的X轴的偏移量
* yOffset:相对于当前位置的Y轴的偏移量
*/
toast.setGravity(Gravity.CENTER, 100, -100);
//获取Toast的布局
LinearLayout laout= (LinearLayout) toast.getView();
//创建view对象
ImageView img=new ImageView(mContext);
img.setImageResource(R.mipmap.ic_launcher);
/**
* 将view添加到toast的布局中
* 第一个参数:是创建的view对象
* 第二个参数:索引,控制view的显示位置
*/
laout.addView(img,0);
toast.show();
2. 自定义Toast
1)自定义布局
使用LayoutInflater获取view对象
LayoutInflater inflater = LayoutInflater.from(mContext);
View toast_view = inflater.inflate(R.layout.toast_layout, null);
2)创建toast对象,设置View
Toast toast = new Toast(Context);
toast.setView(toast_view);
3)设置Toast的显示位置
4)show()
Toast toast = Toast.makeText(mContext, "我是Toast", Toast.LENGTH_SHORT);
/**
* gravity:布局
* xOffset:相对于当前位置的X轴的偏移量
* yOffset:相对于当前位置的Y轴的偏移量
*/
toast.setGravity(Gravity.CENTER, 100, -100);
//获取Toast的布局
LinearLayout laout= (LinearLayout) toast.getView();
//创建view对象
ImageView img=new ImageView(mContext);
img.setImageResource(R.mipmap.ic_launcher);
/**
* 将view添加到toast的布局中
* 第一个参数:是创建的view对象
* 第二个参数:索引,控制view的显示位置
*/
laout.addView(img,0);
toast.show();
2. 自定义Toast
1)自定义布局
使用LayoutInflater获取view对象
LayoutInflater inflater = LayoutInflater.from(mContext);
View toast_view = inflater.inflate(R.layout.toast_layout, null);
2)创建toast对象,设置View
Toast toast = new Toast(Context);
toast.setView(toast_view);
3)设置Toast的显示位置
4)show()