新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android中怎么使用Toast自定义图片和文字

Android中怎么使用Toast自定义图片和文字?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

建昌ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!

自定义工具类代码

 
public class ToastUtils {
 
  private static Context mContext = OcreanSonicApplication.getContext();
 
  public static void showToast(String toast) {
    Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
  }
 
  /**
   * 带图片的吐司提示
   * @param text
   */
  public static void showCustomImgToast(String text) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View view = inflater.inflate(R.layout.toast_view, null);
    ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
    imageView.setBackgroundResource(R.mipmap.pd_ic_finish);
    TextView t = (TextView) view.findViewById(R.id.toast_text);
    t.setText(text);
    Toast toast = null;
    if (toast != null) {
      toast.cancel();
    }
    toast = new Toast(mContext);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(view);
    toast.show();
  }
 
  /**
   * 带图片的吐司提示
   * 通过参数传递,可是设置吐司的图片和文字内容
   * @param text
   */
  public static void showCustomImgToast(String text,int imgResId) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View view = inflater.inflate(R.layout.toast_view, null);
    ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
    imageView.setBackgroundResource(R.mipmap.pd_ic_finish);
    TextView t = (TextView) view.findViewById(R.id.toast_text);
    t.setText(text);
    Toast toast = null;
    if (toast != null) {
      toast.cancel();
    }
    toast = new Toast(mContext);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(view);
    toast.show();
  }
 
  /**
   * 不带图片的吐司提示
   * @param text
   */
  public static void showCustomToast(String text) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View view = inflater.inflate(R.layout.toast_view, null);
    ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
    imageView.setVisibility(View.GONE);
    TextView t = (TextView) view.findViewById(R.id.toast_text);
    t.setText(text);
    Toast toast = null;
    if (toast != null) {
      toast.cancel();
    }
    toast = new Toast(mContext);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(view);
    toast.show();
  }
 
  /**
   * 带图片的吐司,设置吐司弹出的位置为屏幕中心
   * @param text
   */
  public static void showCustomToastCenter(String text) {
    showCustomToastCenter(text, R.mipmap.pd_ic_finish);
  }
 
  /**
   * 带图片的吐司,设置吐司弹出的位置为屏幕中心
   * 通过参数传递,可是设置吐司的图片和文字内容
   * @param text
   */
  public static void showCustomToastCenter(String text, int imgResId) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View view = inflater.inflate(R.layout.toast_view, null);
    ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
    imageView.setBackgroundResource(imgResId);
    TextView t = (TextView) view.findViewById(R.id.toast_text);
    t.setText(text);
    Toast toast = null;
    if (toast != null) {
      toast.cancel();
    }
    toast = new Toast(mContext);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(view);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();
  }
}

在自定义Toast中引用xml布局,用来放置图片和文字,设置id,可以任意在Java代码中设置



 
  
  
    
    
 
    
  
 

关于Android中怎么使用Toast自定义图片和文字问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


新闻标题:Android中怎么使用Toast自定义图片和文字
当前地址:http://www.wjwzjz.com/article/jodeoo.html
在线咨询
服务热线
服务热线:028-86922220
TOP