1.获得Toast view 添加view
Toast toast = Toast.makeText(MainActivity.this, "自定义View", Toast.LENGTH_SHORT);
LinearLayout linearLayout = (LinearLayout) toast.getView();
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(R.mipmap.ic_launcher);
linearLayout.addView(image);
toast.show();
Toast.makeText源码:
public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
2.自定义Toast view
Toast toast=new Toast(context);
View viewMain = LayoutInflater.from(context).inflate(R.layout.activity_main, null);
toast.setView(viewMain);
toast.show();