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

博客介绍了Android开发中带图标Toast工具方法,包含样式、文件、工具类及调用等内容,还给出自定义显示时间的方法。同时展示了简易Toast工具方法的代码实现,如在不同Activity中显示不同时长Toast。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 带图标Toast工具方法1

 

 

public static void show(Context act, String strContent, int iResouce){
    try {

        Toast toast = new Toast(act);
        View view = LayoutInflater.from(act).inflate(R.layout.layout_dialog_toastok, null);

        TextView tvContent = view.findViewById(R.id.content);
        tvContent.setText(strContent);
        ImageView imageView = view.findViewById(R.id.image);
        imageView.setImageResource(iResouce);

        //根据自己需要对view设置text或其他样式
        toast.setView(view);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();



    } catch (Exception e) {
        e.printStackTrace();
    }
}

样式

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="10dp" />
    <solid android:color="@color/color_a000" />

</shape>
<!--toast半透明色-->
<color name="color_a000">#aa000000</color>

文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    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" />


</LinearLayout>

工具类

/**
 * 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、<color name="numtext">#999999</color>

2、

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 设置透明背景色 -->
    <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" />

</shape>

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>

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

    /**
     *  带图片的吐司提示
     *  通过参数传递,可是设置吐司的图片和文字内容
     * @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.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();
    }

    /**
     *  不带图片的吐司提示
     * @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 ,Context context) {
        showCustomToastCenter(text, R.mipmap.em_dx_checkbox_on,context);
    }

    /**
     * 带图片的吐司,设置吐司弹出的位置为屏幕中心
     * 通过参数传递,可是设置吐司的图片和文字内容
     * @param text
     */
    public static void showCustomToastCenter(String text, int imgResId,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(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.BOTTOM, 0, 50);
        toast.show();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值