自定义Toast

代码:

public class MyToast {

 private static MyToast toast;
    private Toast toastwidget;
    private TextView tv;

    public MyToast() {
        if (MyApplication.get() != null) {
            if (toastwidget == null) {
                toastwidget = new Toast(MyApplication.get());
            }
            LayoutInflater inflater = (LayoutInflater) MyApplication.get()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (inflater != null) {
                View toastRoot = inflater.inflate(R.layout.my_toast, null);
                toastwidget.setView(toastRoot);
                tv = (TextView) toastRoot.findViewById(R.id.tv_toast_msg);
            }
        }
    }

    public static MyToast getInstance() {
        if (toast == null) {
            toast = new MyToast();
        }
        return toast;
    }

    public void show(int resid) {
        if (tv != null && toastwidget != null) {
            tv.setText(resid);
            toastwidget.setDuration(android.widget.Toast.LENGTH_SHORT);
            toastwidget.show();
        }
    }

    public void show(String message) {
        if (tv != null && toastwidget != null) {
            tv.setText(message);
            toastwidget.setDuration(android.widget.Toast.LENGTH_SHORT);
            toastwidget.show();
        }
    }

   public void showLong(String message) {
        if (tv != null && toastwidget != null) {
            tv.setText(message);
            toastwidget.setDuration(android.widget.Toast.LENGTH_LONG);
            toastwidget.show();
        }

    }

    public void showLong(int resid) {
        if (tv != null && toastwidget != null) {
            tv.setText(resid);
            toastwidget.setDuration(android.widget.Toast.LENGTH_LONG);
            toastwidget.show();
        }
    }

}

布局:

<?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的源码分析 1. Toast的调用显示 学过Android的人都知道,弹出一个系统API吐司...
    笑说余生阅读 5,811评论 8 46
  • 最近又被拉起做安卓的活了,今天碰到一个功能,要求用自定义Toast,在网上搜了一下,试了好几个,终于有一个能用了,...
    白石火星阅读 791评论 0 5
  • 在安卓应用中Toast的使用频率是非常高的,关于Toast的基本使用这里就不再啰嗦,这里只说一下怎么去更改Toas...
    CnPeng阅读 6,832评论 -1 20
  • 最近在一个项目的开发中有一个需要自定义Toast样式的需求,起初想想应该不复杂,不过在写代码的时候就发现不能改变T...
    Alden_Lei阅读 16,452评论 3 12
  • (转载) 30:教育孩子的主角 谁是教育孩子的猪脚??老师?隔代?孩子的父母? 想对那些说自己没时间的父母说“要想...
    majyer阅读 156评论 0 0