自定义Toast

来源:互联网 发布:2016年就业数据 编辑:程序博客网 时间:2024/06/04 22:46

转载自:http://blog.csdn.net/yongxinzhenxi/article/details/25069415

自定义Toast

public class ToastView {    private static ToastView toastView;    private Toast toast;    private ToastView() {    }    public static ToastView createToastConfig(){        if (toastView==null){            toastView=new ToastView();        }        return toastView;    }    public void ToastShow(Context context, ViewGroup root, String sString){        View view= LayoutInflater.from(context).inflate(R.layout.toast_style,root);        TextView text= (TextView) view.findViewById(R.id.toast);        text.setText(sString);        toast=new Toast(context);        toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL,0,100);        toast.setDuration(Toast.LENGTH_LONG);        toast.setView(view);        toast.show();    }}
Toast的样式设置:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/toast_layout"    android:layout_width="match_parent"    android:background="@drawable/toast_bg"    android:layout_height="match_parent">    <TextView        android:id="@+id/toast"        android:layout_width="200dip"        android:layout_height="30dip"        android:gravity="center"        android:textColor="#ffffff"        android:textSize="18sp" /></LinearLayout>
背景设置:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <corners android:radius="20dip"/>    <solid android:color="#00ff00"/></shape>
调用:

private ToastView tostView;tostView=ToastView.createToastConfig();tostView.ToastShow(MainActivity.this,(ViewGroup) findViewById(R.id.toast_layout),"你好哇,李银河!!!");






0 0
原创粉丝点击