自定义Toast 吐出我的风格

第一步,创建一个Toast将要展示的View


  
  
      
  
  
  
第二步, 编写一个工具类

/**
 * Created by ruiyi on 2016/5/27.
 */
public class ToastUtils {
    private static ToastUtils toastUtils;

    private Toast toast;

    private ToastUtils(){
    }

    public static ToastUtils createToastConfig(){
        if (toastUtils==null) {
            toastUtils = new ToastUtils();
        }
        return toastUtils;
    }

    public void ToastShow(Context context,ViewGroup root,String tvString){
        View layout = LayoutInflater.from(context).inflate(R.layout.toast_xml,root);
        TextView text = (TextView) layout.findViewById(R.id.tv);
        ImageView mImageView = (ImageView) layout.findViewById(R.id.iv);
        mImageView.setBackgroundResource(R.drawable.ic_launcher);
        text.setText(tvString);
        text.setTextColor(R.color.green_01);
        toast = new Toast(context);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }
}

第三步,使用

ToastUtils createToastConfig().ToastShow(MainActivity.this, (ViewGroup)findViewById(R.id.toast_layout_root), "你好");
 
  
就这样

你可能感兴趣的:(android_实战)