
Toast.makeText(getApplicationContext(), "默认Toast样式", 2000).show();

- toast = Toast.makeText(getApplicationContext(), "自定义位置Toast", 2000);
- toast.setGravity(Gravity.CENTER, 0, 0);// 设置居中
- toast.show();
toast = Toast.makeText(getApplicationContext(), "自定义位置Toast", 2000); toast.setGravity(Gravity.CENTER, 0, 0);// 设置居中 toast.show();

- toast = Toast.makeText(getApplicationContext(), "带图片的Toast", 2000);
- LinearLayout toastView = (LinearLayout) toast.getView();// 获得toast的视图,并强制转化为LinearLayout
- ImageView imageView = new ImageView(getApplicationContext());
- imageView.setImageResource(R.drawable.icon);
- toastView.addView(imageView, 0);/* 将ImageView添加进视图 */
- toast.show();
toast = Toast.makeText(getApplicationContext(), "带图片的Toast", 2000); LinearLayout toastView = (LinearLayout) toast.getView();// 获得toast的视图,并强制转化为LinearLayout ImageView imageView = new ImageView(getApplicationContext()); imageView.setImageResource(R.drawable.icon); toastView.addView(imageView, 0);/* 将ImageView添加进视图 */ toast.show();

- LayoutInflater inflater = getLayoutInflater();
- View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast));
- ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
- image.setImageResource(R.drawable.icon);
- TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
- title.setText("标题:自定义");
- TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
- text.setText("完全自定义Toast");
- toast = new Toast(getApplicationContext());
- toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
- toast.setDuration(2000);
- toast.setView(layout);
- toast.show();
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("标题:自定义");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(2000);
toast.setView(layout);
toast.show();

- public class MyToast {
- public static void myToastShow(Context context, int imageResId,// 图片的ID
- String content,// 显示的内容
- int duration) {
- Toast toast = new Toast(context);
- toast.setDuration(duration);
- toast.setGravity(Gravity.BOTTOM, 0, 10);// Toast的位置
- LinearLayout toastLayout = new LinearLayout(context);
- toastLayout.setOrientation(LinearLayout.HORIZONTAL);
- toastLayout.setGravity(Gravity.CENTER_VERTICAL);
- ImageView imageView = new ImageView(context);
- imageView.setImageResource(imageResId);
- TextView tv_content = new TextView(context);
- tv_content.setText(content);
- tv_content.setTextColor(Color.BLACK);
- tv_content.setBackgroundColor(Color.TRANSPARENT);// 透明背景
- toastLayout.addView(imageView);
- toastLayout.addView(tv_content);
- toast.setView(toastLayout);
- toast.show();
- }
- }
public class MyToast {
public static void myToastShow(Context context, int imageResId,// 图片的ID
String content,// 显示的内容
int duration) {
Toast toast = new Toast(context);
toast.setDuration(duration);
toast.setGravity(Gravity.BOTTOM, 0, 10);// Toast的位置
LinearLayout toastLayout = new LinearLayout(context);
toastLayout.setOrientation(LinearLayout.HORIZONTAL);
toastLayout.setGravity(Gravity.CENTER_VERTICAL);
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageResId);
TextView tv_content = new TextView(context);
tv_content.setText(content);
tv_content.setTextColor(Color.BLACK);
tv_content.setBackgroundColor(Color.TRANSPARENT);// 透明背景
toastLayout.addView(imageView);
toastLayout.addView(tv_content);
toast.setView(toastLayout);
toast.show();
}
}
调用实例:
MyToast.myToastShow(ShowAppActivity.this, R.drawable.grids,"网格显示", Toast.LENGTH_SHORT);

浙公网安备 33010602011771号