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

本文介绍了如何在Android中创建一个自定义的Toast,包括设置图标、文字内容以及自定义显示时间。提供了`ToastUtil`工具类的实现,包括不同长度的显示方法,以及使用Timer进行延时展示和取消的方法。示例代码中展示了如何调用这些工具方法来自定义显示Toast。
摘要由CSDN通过智能技术生成

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

imageVie

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值