自定義Toast的背景顏色大小及字型大小

adan_xp發表於2019-08-16

參考文章 原文連結:http://download.csdn.net/detail/u010566681/9612322

文章內容的核心思路是通過Toast類的getView方法獲取到Toast的View,通過得到的view.findbyid查詢安卓系統為Toast定義的TextView資源(android.R.id.message)。

而後針對TextView類進行操作即可。

更新程式碼也封裝進了原作者的ToastUtil類中,使用時首先通過

ToastUtil toastUtil = new ToastUtil();  //建立toastUtil物件
        toastUtil.Short(context,getResources().getString(id))  //呼叫Short方法傳入提示內容
                .setToastBackground(Color.WHITE,R.drawable.toast_radius)  //呼叫原文的setToastBackground方法設定顏色及背景
                .setToastSize(400,200,24).show();  //呼叫新增的setToastSize設定Toast的寬,高及字型大小。

對比原來的類增加設定Toast大小及字型大小的類:


'''

/*
* 設定Toast大小
* */
public ToastUtil setToastSize(int width,int height,int textSize){
View view = toast.getView();
if(view != null){
TextView message = view.findViewById(android.R.id.message);
message.setWidth(width);
message.setHeight(height);
message.setGravity(Gravity.CENTER);
if(textSize<20)
message.setTextSize(20);
else
message.setTextSize(textSize);
// message.setAutoSizeTextTypeWithDefaults(AUTO_SIZE_TEXT_TYPE_UNIFORM);
}
return this;
}

'''

 

完整類程式碼如下:

'''

package com.telecom.geoquiz;

/**
 * Created by apple on 16/8/24.
 */
import android.content.Context;
import android.graphics.Color;
import android.graphics.fonts.FontVariationAxis;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import static android.widget.TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM;

/**
 * 作者:ban on 16/8/24 17:55
 */
public class ToastUtil {

    private Toast toast;
    private LinearLayout toastView;

    /**
     * 修改原佈局的Toast
     */
    public ToastUtil() {

    }

    /**
     * 完全自定義佈局Toast
     *
     * @param context
     * @param view
     */
    public ToastUtil(Context context, View view, int duration) {
        toast = new Toast(context);
        toast.setView(view);
        toast.setDuration(duration);
    }

    /**
     * 向Toast中新增自定義view
     *
     * @param view
     * @param postion
     * @return
     */
    public ToastUtil addView(View view, int postion) {
        toastView = (LinearLayout) toast.getView();
        toastView.addView(view, postion);

        return this;
    }

    /**
     * 設定Toast字型及背景顏色
     *
     * @param messageColor
     * @param backgroundColor
     * @return
     */
    public ToastUtil setToastColor(int messageColor, int backgroundColor) {
        View view = toast.getView();
        //設定Toast背景顏色為透明
        view.setBackgroundColor(Color.TRANSPARENT);
        if (view != null) {
            TextView message = ((TextView) view.findViewById(android.R.id.message));
            message.setBackgroundColor(backgroundColor);
            message.setTextColor(messageColor);
        }
        return this;
    }

    /**
     * 設定Toast字型顏色及背景
     *
     * @param messageColor
     * @param background
     * @return
     */
    public ToastUtil setToastBackground(int messageColor, int background) {
        View view = toast.getView();
        //設定Toast背景顏色為透明
        view.setBackgroundColor(Color.TRANSPARENT);
        if (view != null) {
            TextView message = ((TextView) view.findViewById(android.R.id.message));
            message.setBackgroundResource(background);
            message.setTextColor(messageColor);
        }
        return this;
    }

    /*
    * 設定Toast大小
    * */
    public ToastUtil setToastSize(int width,int height,int textSize){
        View view = toast.getView();
        if(view != null){
            TextView message = view.findViewById(android.R.id.message);
            message.setWidth(width);
            message.setHeight(height);
            message.setGravity(Gravity.CENTER);
            if(textSize<20)
                message.setTextSize(20);
            else
                message.setTextSize(textSize);
//            message.setAutoSizeTextTypeWithDefaults(AUTO_SIZE_TEXT_TYPE_UNIFORM);
        }
        return this;
    }

    /**
     * 短時間顯示Toast
     */
    public ToastUtil Short(Context context, CharSequence message) {
        if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
            toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
            toastView = null;
        } else {
            toast.setText(message);
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        return this;
    }

    /**
     * 短時間顯示Toast
     */
    public ToastUtil Short(Context context, int message) {
        if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
            toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
            toastView = null;
        } else {
            toast.setText(message);
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        return this;
    }

    /**
     * 長時間顯示Toast
     */
    public ToastUtil Long(Context context, CharSequence message) {
        if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
            toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
            toastView = null;
        } else {
            toast.setText(message);
            toast.setDuration(Toast.LENGTH_LONG);
        }
        return this;
    }

    /**
     * 長時間顯示Toast
     *
     * @param context
     * @param message
     */
    public ToastUtil Long(Context context, int message) {
        if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
            toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
            toastView = null;
        } else {
            toast.setText(message);
            toast.setDuration(Toast.LENGTH_LONG);
        }
        return this;
    }

    /**
     * 自定義顯示Toast時間
     *
     * @param context
     * @param message
     * @param duration
     */
    public ToastUtil Indefinite(Context context, CharSequence message, int duration) {
        if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
            toast = Toast.makeText(context, message, duration);
            toastView = null;
        } else {
            toast.setText(message);
            toast.setDuration(duration);
        }
        return this;
    }

    /**
     * 自定義顯示Toast時間
     *
     * @param context
     * @param message
     * @param duration
     */
    public ToastUtil Indefinite(Context context, int message, int duration) {
        if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
            toast = Toast.makeText(context, message, duration);
            toastView = null;
        } else {
            toast.setText(message);
            toast.setDuration(duration);
        }
        return this;
    }

    /**
     * 顯示Toast
     *
     * @return
     */
    public ToastUtil show() {
        toast.show();

        return this;
    }

    /**
     * 獲取Toast
     *
     * @return
     */
    public Toast getToast() {
        return toast;
    }

}

'''

相關文章