Toast特效

Java代码 复制代码 收藏代码
  1. Toast.makeText(getApplicationContext(), "默认Toast样式"2000).show();  
Toast.makeText(getApplicationContext(), "默认Toast样式", 2000).show();

Java代码 复制代码 收藏代码
  1. toast = Toast.makeText(getApplicationContext(), "自定义位置Toast"2000);   
  2. toast.setGravity(Gravity.CENTER, 00);// 设置居中   
  3. toast.show();  
toast = Toast.makeText(getApplicationContext(), "自定义位置Toast", 2000);
toast.setGravity(Gravity.CENTER, 0, 0);// 设置居中
toast.show();

Java代码 复制代码 收藏代码
  1. toast = Toast.makeText(getApplicationContext(), "带图片的Toast"2000);   
  2. LinearLayout toastView = (LinearLayout) toast.getView();// 获得toast的视图,并强制转化为LinearLayout   
  3. ImageView imageView = new ImageView(getApplicationContext());   
  4. imageView.setImageResource(R.drawable.icon);   
  5. toastView.addView(imageView, 0);/* 将ImageView添加进视图 */  
  6. 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();

Java代码 复制代码 收藏代码
  1.                                              LayoutInflater inflater = getLayoutInflater();   
  2. View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast));   
  3.   
  4. ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);   
  5. image.setImageResource(R.drawable.icon);   
  6.   
  7. TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);   
  8. title.setText("标题:自定义");   
  9.   
  10. TextView text = (TextView) layout.findViewById(R.id.tvTextToast);   
  11. text.setText("完全自定义Toast");   
  12.   
  13. toast = new Toast(getApplicationContext());   
  14. toast.setGravity(Gravity.RIGHT | Gravity.TOP, 1240);   
  15. toast.setDuration(2000);   
  16. toast.setView(layout);   
  17. 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();

Java代码 复制代码 收藏代码
  1. public class MyToast {   
  2.   
  3.     public static void myToastShow(Context context, int imageResId,// 图片的ID   
  4.             String content,// 显示的内容   
  5.             int duration) {   
  6.         Toast toast = new Toast(context);   
  7.         toast.setDuration(duration);   
  8.         toast.setGravity(Gravity.BOTTOM, 010);// Toast的位置   
  9.   
  10.         LinearLayout toastLayout = new LinearLayout(context);   
  11.         toastLayout.setOrientation(LinearLayout.HORIZONTAL);   
  12.         toastLayout.setGravity(Gravity.CENTER_VERTICAL);   
  13.   
  14.         ImageView imageView = new ImageView(context);   
  15.         imageView.setImageResource(imageResId);   
  16.   
  17.         TextView tv_content = new TextView(context);   
  18.         tv_content.setText(content);   
  19.         tv_content.setTextColor(Color.BLACK);   
  20.         tv_content.setBackgroundColor(Color.TRANSPARENT);// 透明背景   
  21.         toastLayout.addView(imageView);   
  22.         toastLayout.addView(tv_content);   
  23.   
  24.         toast.setView(toastLayout);   
  25.         toast.show();   
  26.   
  27.     }   
  28.   
  29. }  
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();

	}

}

 调用实例:

Java代码 复制代码 收藏代码
  1. MyToast.myToastShow(ShowAppActivity.this, R.drawable.grids,"网格显示", Toast.LENGTH_SHORT);  
MyToast.myToastShow(ShowAppActivity.this, R.drawable.grids,"网格显示", Toast.LENGTH_SHORT);
  • 大小: 25.3 KB
  • 大小: 27.1 KB
  • 大小: 28.7 KB
  • 大小: 25.9 KB
  • 大小: 40.9 KB
原文地址:https://www.cnblogs.com/qingblog/p/2544610.html