美文网首页
自定义Toast

自定义Toast

作者: Anivia_Hanger | 来源:发表于2019-11-07 18:08 被阅读0次

代码:

public class MyToast {

private static MyToastmyMyToast;

    private ViewtoastRoot;

    private Toasttoast;

    private TextViewtv;

    private MyToast(Context context) {

toastRoot = ((Activity) context).getLayoutInflater().inflate(R.layout.my_toast, null);

        toast =new Toast(context.getApplicationContext());

        toast.setView(toastRoot);

        tv = (TextView)toastRoot.findViewById(R.id.tv_toast_msg);

    }

public static void showToast(Context context, int resid) {

String message = context.getString(resid);

        showToast(context, message);

    }

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

showToast(context, message, Toast.LENGTH_SHORT);

    }

private static void showToast(Context context, String message, int length) {

if (myMyToast ==null) {

myMyToast =new MyToast(context);

        }

myMyToast.show(message, length);

    }

private void show(String message, int length) {

tv.setText(message);

        toast.setDuration(length);

        toast.show();

    }

public static void showLongToast(Context context, int resId) {

String s = context.getString(resId);

        showLongToast(context, s);

    }

public static void showLongToast(Context context, String s) {

showToast(context, s, Toast.LENGTH_LONG);

    }

}

布局:

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

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

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_gravity="center"

    android:gravity="center"

    android:background="@drawable/tips">

        android:id="@+id/tv_toast_msg"

        android:gravity="center"

        android:layout_height="wrap_content"

        android:layout_width="wrap_content"

        android:layout_marginLeft="35dp"

        android:layout_marginRight="35dp"

        android:text="1234"

        android:textSize="35sp"

        android:textColor="#FF0C0D13"

        android:layout_gravity="center_vertical"

    />

</LinearLayout>

Drawable:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

            <!-- 第一层 -->


                    <solid android:color="#00a1bd"/>

                    <corners android:radius="100dp"/>

                    <size android:height="100dp" android:width="600dp">

            <!-- 第二层 -->

            <item android:bottom="6dp" android:right="6dp" android:top="6dp" android:left="6dp">

                    <solid android:color="#00d7fc"/>

                    <corners android:radius="88dp"/>

</selector>

相关文章

  • Toast(自定义Toast)

    默认的Toast: 自定义Toast(自定义图标样式\显示时间\位置): 效果图: 代码:

  • Toast和AlertDialog通用工具

    (1)自定义Toast (2)自定义对话框 (3)二合一 这里涉及到Toast, 使用Toast之前必须先判断是否...

  • Apicloud功能模块使用customToast

    /*Title: customToastDescription: 自定义Toast,让Toast更完美的融入应用,...

  • Tween实战-Toast动画

    自定义Toast 这里并不是创建一个自定义Toast,而是把toast添加到一个window中,对这个window...

  • Android——Toast

    三、使用 1.Toast使用技巧 Toast使用技巧 (1)自定义 Toast 布局 (2)使用 Applicat...

  • Android 自定义Toast

    Android自定义Toast Toast的基础用法 Toast显示的位置通常情况下Toast显示在整个界面的底部...

  • ToastUtil工具类

    自定义toast,解决系统toast连续弹出,长时间不消失的问题

  • Android 带图片的Toast

    Android 带图片的Toast Github代码 带图片的Toast,其实就是自定义布局的Toast,只要T...

  • 自定义小火箭

    ####自定义归属地的Toast 1. 在构造方法,构建Toast的params > 这段代码是从Toast的内部...

  • Android菜鸟起飞|Toast的高级使用

    Toast高级用法之设置带图片的Toast和自定义Toast 虽然google推出了更加美观实用的snackBar...

网友评论

      本文标题:自定义Toast

      本文链接:https://www.haomeiwen.com/subject/mogvbctx.html