自定义Toast

来源:互联网 发布:it狂人类似 编辑:程序博客网 时间:2024/05/29 16:50

布局类:

<RelativeLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:android="http://schemas.android.com/apk/res/android">    <TextView        android:id="@+id/textView1"        android:layout_width="300dp"        android:layout_centerInParent="true"        android:background="@drawable/btn_white_shape_selector"        android:layout_height="200dp"        android:textColor="@color/black"/></RelativeLayout>
样式自定义:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="#FFFFFF" />    <corners android:topLeftRadius="10dp"        android:topRightRadius="10dp"        android:bottomRightRadius="10dp"        android:bottomLeftRadius="10dp"/>    <stroke android:width="1dp" android:color="#FFFFFF" /></shape>
自定义Toast类:

/** * 自定义Toast工具类 */public class MyToast {    public  Toast mToast;    public MyToast(Context context, CharSequence text, int duration) {        View v = LayoutInflater.from(context).inflate(R.layout.eplay_toast, null);        TextView textView = (TextView) v.findViewById(R.id.textView1);        textView.setText(text);        mToast = new Toast(context);        mToast.setDuration(duration);        mToast.setView(v);    }    public  static MyToast makeText(Context context, CharSequence text, int duration) {        return new MyToast(context, text, duration);    }    public void show() {        if (mToast != null) {            mToast.show();        }    }}
Activity中调用:

MyToast.makeText(getApplicationContext(), "反馈成功!", Toast.LENGTH_SHORT).show();