android Toast 彈出在屏幕中間位置以及自定義Toast

Toast 我想咱們應該使用的都不少,通常咱們使用默認設置較多,可是默認設置每每不能知足咱們的需求,那咱們如今來自定義下:html

默認Toast:java

Toast.makeText(MainActivity.this,"點擊按鈕",Toast.LENGTH_SHORT).show();

設置Toast位置:android

          經過setGravity設置Toast位置,能夠是 佈局

 Gravity.CENTER:中間this

 Gravity.BOTTOM:下方spa

 Gravity.TOP:上方code

 Gravity.RIGHT:右邊htm

Gravity.LEFT:左邊圖片

Toast toast = Toast.makeText(getApplicationContext(), "點擊按鈕", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();

Toast 也能夠是個佈局:這個佈局裏能夠是任何控件  圖片 文字 等ip

Toast toast2 = new Toast(MainActivity.this);
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_custom, null);
                toast2.setView(view);
                toast2.setGravity(Gravity.CENTER, 0, 0);
                toast2.show();

下面附上一個 ToastUtil類:

public class ToastUitl {


    private static Toast toast;
    private static Toast toast2;

    /**
     * 初始化Toast(消息,時間)
     */
    private static Toast initToast(CharSequence message, int duration) {
        if (toast == null) {
            toast = Toast.makeText(BaseApplication.getAppContext(), message, duration);
        } else {
            //設置文字
            toast.setText(message);
            //設置存續期間
            toast.setDuration(duration);
        }
        return toast;
    }

    /**
     * 短期顯示Toast(消息 String等)
     */
    public static void showShort(CharSequence message) {
        initToast(message, Toast.LENGTH_SHORT).show();
    }


    /**
     * 短期顯示Toast(資源id)
     */
    public static void showShort(int strResId) {
        initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast.LENGTH_SHORT).show();
    }

    /**
     * 長時間顯示Toast(消息 String等)
     */
    public static void showLong(CharSequence message) {
        initToast(message, Toast.LENGTH_LONG).show();
    }

    /**
     * 長時間顯示Toast(資源id)
     */
    public static void showLong(int strResId) {
        initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast.LENGTH_LONG).show();
    }

    /**
     * 自定義顯示Toast時間(消息 String等,時間)
     */
    public static void show(CharSequence message, int duration) {
        initToast(message, duration).show();
    }

    /**
     * 自定義顯示Toast時間(消息 資源id,時間)
     */
    public static void show(int strResId, int duration) {
        initToast(BaseApplication.getAppContext().getResources().getText(strResId), duration).show();
    }

    /**
     * 顯示有image的toast 這是個view
     */
    public static Toast showToastWithImg(final String tvStr, final int imageResource) {
        if (toast2 == null) {
            toast2 = new Toast(BaseApplication.getAppContext());
        }
        View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.toast_custom, null);
        TextView tv = (TextView) view.findViewById(R.id.toast_custom_tv);
        tv.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr);
        ImageView iv = (ImageView) view.findViewById(R.id.toast_custom_iv);
        if (imageResource > 0) {
            iv.setVisibility(View.VISIBLE);
            iv.setImageResource(imageResource);
        } else {
            iv.setVisibility(View.GONE);
        }
        toast2.setView(view);
        toast2.setGravity(Gravity.CENTER, 0, 0);
        toast2.show();
        return toast2;

    }
}

對應的佈局文件:此佈局文件根據本身需求自定義

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:orientation="vertical">
            <ImageView
                android:layout_marginTop="10dp"
                android:id="@+id/toast_custom_iv"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:src="@mipmap/ic_launcher"
                android:layout_gravity="center"/>

            <TextView

                android:id="@+id/toast_custom_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:textSize="16sp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginBottom="10dp"
                android:textColor="#000"
                tools:text="點擊toast" />
        </LinearLayout>


補加:

自定義Toast 填充滿整個屏幕:

Toast toast2 = new Toast(MainActivity.this);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_custom, null);
ImageView iv_toast = (ImageView) view.findViewById(R.id.iv_toast);
TextView tv_toast = (TextView) view.findViewById(R.id.tv_toast);
toast2.setView(view);
toast2.setGravity(Gravity.FILL_HORIZONTAL | Gravity.VERTICAL_GRAVITY_MASK, 0, 0);
toast2.show();
注意:佈局文件要
android:layout_width="match_parent" android:layout_height="match_parent"