使用自定义Toast是为了和app的特性统一,因为本身的Toast的背景颜色是黑色的,字体是白色的,所以在你的app显示时就有可能出现不协调.因此自定义可以解决这个问题
用法
使用时先根据上下文 make(context)
然后调用各个参数来设置属性
最后别忘了调用show()方法来显示
1 //使用示例,使用指定背景图显示 2 3 CustomToast.make(getApplicationContext()).setDuration(0) .setBackground(R.drawable.ic_logo).setText("自定义的toast").show(); 4 5 6 //默认显示 7 CustomToast.make(getApplicationContext()).setText("toast啊").show(); 8
不多说,直接上代码
布局文件,这个可以你随便怎么用 都可以,在java代码中要各种设置就行
1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- 自定义的Toast的样式布局 --> 3 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="wrap_content" 5 android:layout_height="wrap_content" 6 android:layout_gravity="center_horizontal" 7 android:background="#66318fad" 8 android:padding="5dp" > 9 10 <!-- 可以设置一个ImageView作为背景 --> 11 12 <ImageView 13 android:id="@+id/ivForToast" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_centerInParent="true" 17 android:contentDescription="@string/image_desc" 18 android:visibility="visible" /> 19 <!-- 显示文字 --> 20 21 <TextView 22 android:id="@+id/tvForToast" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_centerInParent="true" 26 android:textColor="#ffffff" 27 android:textSize="20sp" /> 28 29 </RelativeLayout>
java代码:
1 /** 2 * @file CustomToast.java 3 * @create 下午8:46:22 4 * @author act262 5 * @description 自定义的Toast 6 */ 7 package com.act262.app.widget; 8 9 import android.content.Context; 10 import android.graphics.drawable.Drawable; 11 import android.view.Gravity; 12 import android.view.LayoutInflater; 13 import android.view.View; 14 import android.widget.ImageView; 15 import android.widget.TextView; 16 import android.widget.Toast; 17 18 import com.act262.app.R; 19 import com.act262.app.utils.LogUtil; 20 21 public class CustomToast { 22 public Context mContext; 23 public ImageView imageView; 24 public TextView textView; 25 public Toast mToast; 26 public View rootView; 27 public static CustomToast mCustomToast; 28 29 private CustomToast(Context context) { 30 if (mToast == null) { 31 LogUtil.v("CustomToast", "CustomToast new "); 32 rootView = LayoutInflater.from(context).inflate( 33 R.layout.custom_toast_layout, null); 34 initView(); 35 mToast = new Toast(context); 36 // mToast.setView(rootView); 37 // defaultPro(); 38 } else { 39 LogUtil.v("CustomToast", "CustomToast not null "); 40 } 41 42 } 43 44 private void initView() { 45 imageView = (ImageView) rootView.findViewById(R.id.ivForToast); 46 textView = (TextView) rootView.findViewById(R.id.tvForToast); 47 } 48 49 // 默认设置 50 private void defaultPro() { 51 52 mToast.setDuration(Toast.LENGTH_LONG); 53 mToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0); 54 mToast.setMargin(0, 0); 55 } 56 57 /** 58 * 使用自定义toast时调用该静态方法后再调用设置参数的方法,最后在调用show()来显示 59 * 60 * @param context 61 * @return 62 */ 63 public static CustomToast make(Context context) { 64 mCustomToast = new CustomToast(context); 65 return mCustomToast; 66 } 67 68 /** 69 * 调用其他方法设置后调用这个来显示 70 * 71 * @param context 72 */ 73 public void show() { 74 mToast.setView(rootView); 75 mToast.show(); 76 } 77 78 /** 79 * 设置指定的View 80 * 81 * @param view 82 * @return 83 */ 84 public CustomToast setView(View view) { 85 mToast.setView(rootView); 86 return mCustomToast; 87 } 88 89 /** 90 * 设置toast显示位置 91 * 92 * @param gravity 93 * 对齐位置 94 * @param xOffset 95 * x偏移 96 * @param yOffset 97 * y偏移 98 * @return 99 */ 100 public CustomToast setGravity(int gravity, int xOffset, int yOffset) { 101 mToast.setGravity(gravity, xOffset, yOffset); 102 103 return mCustomToast; 104 } 105 106 /** 107 * 设置外边距 使用长宽的%来表示 108 * 109 * @param horizontalMargin 110 * 水平外边距到toast显示的%距离 111 * @param verticalMargin 112 * @return 113 */ 114 public CustomToast setMargin(float horizontalMargin, float verticalMargin) { 115 mToast.setMargin(horizontalMargin, verticalMargin); 116 return mCustomToast; 117 } 118 119 /** 120 * 设置自定义toast显示时间长短 121 * 122 * @param duration 123 * 0 显示短,1显示长 124 * @return 125 */ 126 public CustomToast setDuration(int duration) { 127 switch (duration) { 128 case 0: 129 mToast.setDuration(Toast.LENGTH_SHORT); 130 break; 131 case 1: 132 mToast.setDuration(Toast.LENGTH_LONG); 133 break; 134 default: 135 mToast.setDuration(Toast.LENGTH_LONG); 136 break; 137 } 138 139 return mCustomToast; 140 } 141 142 /** 设置自定义toast提示文字 */ 143 public CustomToast setText(String text) { 144 // mToast.setText(text); 145 textView.setText(text); 146 textView.setTextSize(16); 147 setDuration(1); 148 return mCustomToast; 149 } 150 151 /** 152 * 设置字体大小 153 * 154 * @param size 155 * @return 156 */ 157 public CustomToast setTextSize(float size) { 158 textView.setTextSize(size); 159 return mCustomToast; 160 } 161 162 /** 设置自定义toast提示文字 */ 163 public CustomToast setText(int textRes) { 164 return setText(mContext.getResources().getString(textRes)); 165 } 166 167 /** 设置自定义toast背景 */ 168 public CustomToast setBackground(int resid) { 169 imageView.setBackgroundResource(resid); 170 imageView.setVisibility(View.VISIBLE); 171 return mCustomToast; 172 } 173 174 /** 设置自定义toast背景 */ 175 public CustomToast setBackground(Drawable drawable) { 176 imageView.setBackground(drawable); 177 imageView.setVisibility(View.VISIBLE); 178 return mCustomToast; 179 } 180 181 /** 182 * 取消该toast显示 183 */ 184 public void cancel() { 185 mToast.cancel(); 186 } 187 }
原文:http://www.cnblogs.com/act262/p/3603842.html