代码:
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>