itgoyo / AndroidSummary

12 stars 4 forks source link

自定义CenterToast #12

Open itgoyo opened 7 years ago

itgoyo commented 7 years ago

ToastManager

public class ToastManager {
    private static ToastManager manager;
    private Toast toast;
    private TextView mMessageView;
    private View mView;

    private ToastManager(Context context) {
        mView = LayoutInflater.from(context).inflate(R.layout.center_toast_layout, null);
        mMessageView = (TextView) mView.findViewById(R.id.message);
        toast = new Toast(context);
        toast.setView(mView);
        toast.setGravity(Gravity.CENTER, 0, 0);
    }

    public static void initManager(Context context) {
        manager = new ToastManager(context);
    }

    public static ToastManager getInstance(){
        if(manager == null)
        {
            initManager(Talk915App.getInstance());
        }
        return manager;
    }

    public void show(String str, int duration) {
        mMessageView.setText(str);
        toast.setDuration(duration);
        toast.show();
    }

    public void dismiss() {
        toast.cancel();
    }

    /**
     *
     * @param context
     * @param str  更多颜色自己自定义
     * @param duration
     */

    public static void BlueToast(Context context,String str, int duration){
        View mView = View.inflate(context, R.layout.view_custoast, null);
        Toast toast = new Toast(context);
        toast.setView(mView);
        toast.setDuration(duration);
//      toast.setGravity(Gravity.BOTTOM, 0, 0);
        TextView mMessageView = (TextView) mView.findViewById(R.id.message);
        mMessageView.setText(str);
        mMessageView.setTextSize(14);
        toast.show();
    }
}

CenterToast

public class CenterToast{
    private Context mContext;

    public CenterToast(Context context) {
        mContext = context;
    }
    public void show(int messageId, int duration){
        ToastManager.getInstance().show(mContext.getString(messageId), duration);
    }

    public void show(String message, int duration){
        ToastManager.getInstance().show(message, duration);
    }

}

blue_toast_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/white" />
    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />

    <stroke android:width="1dp"
        android:color="@color/bluelike"/>
</shape>

center_toast_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/dialog_bg" />
    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />
</shape>

center_toast_layout

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/dialog_bg" />
    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />
</shape>

view_custoast

<?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:background="@drawable/blue_toast_bg"
    android:gravity="center">

    <TextView android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="120dip"
        android:minHeight="50dip"
        android:gravity="center"
        android:textColor="@color/bluelike"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:textSize="17dp"
        />

</LinearLayout>