Android 自定义带图标Toast,工具方法,Toast自定义显示时间

android:background="@drawable/shape_bg_a000_r10"

android:gravity="center"

android:orientation="vertical">





<ImageView

    android:id="@+id/iamge"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginLeft="60dp"

    android:layout_marginRight="60dp"

    android:layout_marginTop="34dp" />



<TextView

    android:id="@+id/tv_content"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_gravity="center_horizontal"

    android:layout_marginBottom="34dp"

    android:layout_marginTop="20dp"

    android:gravity="center"

    android:textColor="@color/white"

    android:textSize="30sp" />



### 工具类



/**

  • Created by meixi on 2020/11/12.

*/

public class ToastUtil {

private static Toast toast;



public static void showToastCenter(Context ctx, String str) {

    Toast toast = Toast.makeText(ctx, str, Toast.LENGTH_SHORT);

    toast.setGravity(0, Gravity.CENTER, Gravity.CENTER);

    toast.show();

}



public static void showToastLong(Context ctx, String str) {

    Toast.makeText(ctx, str, Toast.LENGTH_LONG).show();

}



public static void showToastShort(Context ctx, String str) {

    Toast.makeText(ctx, str, Toast.LENGTH_SHORT).show();

}



public static void showToast(Activity activity, String toastContent, int image,int time) {

    LayoutInflater inflater = activity.getLayoutInflater();

    View layout = inflater.inflate(R.layout.toast_stat_layout, null);

    TextView content = layout.findViewById(R.id.tv_content);

    content.setText(toastContent);

    ImageView imageView = layout.findViewById(R.id.iamge);

    imageView.setImageResource(image);



    if (toast==null){

        toast = new Toast(activity);

    }

    toast.setGravity(Gravity.CENTER, 0, 0);

    toast.setDuration(Toast.LENGTH_LONG);

    toast.setView(layout);

// toast.show();

    showMyToast(toast,time);

}



public static void showMyToast(final Toast toast, final int cnt) {

    final Timer timer =new Timer();

    timer.schedule(new TimerTask() {

        @Override

        public void run() {

            LogPlus.e("lgq","收尾。。show。。");

            toast.show();

        }

    },0,3000);

    new Timer().schedule(new TimerTask() {

        @Override

        public void run() {

            toast.cancel();

            timer.cancel();

        }

    }, cnt );

}

}




### 调用



//自定义显示时间



ToastUtil.showToast(MainActivity.this, “失败”, R.mipmap.ic_failure,30000);




简易Toast工具方法2

============



private void displayToastTip(final String tip) {  

    runOnUiThread(new Runnable() {  

        @Override  

        public void run() {  

            Toast.makeText(SfzShiBieActivity.this, tip, Toast.LENGTH\_SHORT).show();  

        }  

    });  

}  

 



private void toast(final String text) {  

    handler.post(new Runnable() {  

        @Override  

        public void run() {  

            Toast.makeText(RegActivity.this, text, Toast.LENGTH\_LONG).show();  

        }  

    });  

}



private Handler handler = new Handler(Looper.getMainLooper());  

 



背景资源



1、#999999




2、



<?xml version="1.0" encoding="utf-8"?>
<!-- 设置透明背景色 -->

<solid android:color="@color/numtext" />



<!-- 设置一个黑色边框 -->

<stroke

    android:width="2px"

    android:color="@color/numtext" />

<!-- 设置四个圆角的半径 -->

<corners

    android:radius="8dp"/>

<!-- 设置一下边距,让空间大一点 -->

<padding

    android:bottom="7dp"

    android:left="9dp"

    android:right="9dp"

    android:top="7dp" />



3、layout.xml布局



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width="match_parent"

android:layout_height="match_parent"



android:orientation="vertical">



<!-- android:minHeight="80dp"-->

<LinearLayout

    android:layout_width="120dp"

    android:layout_height="80dp"

    android:layout_gravity="center"

    android:background="@drawable/backtoa"



    android:baselineAligned="false"

    android:gravity="center"

    android:orientation="vertical"

    android:padding="5dp">

    <!--android:background="@drawable/toast_bg"-->

    <ImageView

        android:id="@+id/toast_image"

        android:layout_width="30dp"

        android:layout_height="30dp"

        android:layout_gravity="center"

        android:layout_margin="2dp"

        android:background="@mipmap/em_dx_checkbox_on" />



    <TextView

        android:id="@+id/toast_text"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:layout_margin="2dp"

        android:text="保存成功"

        android:textColor="#ffffff"

        android:textSize="15dp" />

</LinearLayout>



4、工具类



/**

  • 作者:created by meixi

  • 邮箱:13164716840@163.com

  • 日期:2018/8/28 08

*/

public class ToastUtils {

private static Context mContext ;



public static void showToast(String toast,Context context) {

    mContext = context;

    Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();

}



/**

 *  带图片的吐司提示

 * @param text

 */

public static void showCustomImgToast(String text ,Context context) {

    mContext = context;

    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.em_dx_checkbox_on);

    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();

}



/**

 *  带图片的吐司提示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值