Android Toast

    /* a toast with style white (white background and black text, ...) */
    public static Toast showToastBackgroundWhite(Context context, CharSequence text) {
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.setGravity(Gravity.CENTER, 0, 0);
        LinearLayout toastLayout = (LinearLayout) toast.getView();
        toastLayout.setBackgroundResource(R.drawable.background_round_white_half_transparent2);
        TextView toastTV = (TextView) toastLayout.getChildAt(0);
        toastTV.setTextColor(Color.BLACK);
        toastTV.setTextSize(40);
        toastTV.setTypeface(null, Typeface.BOLD);
        toastTV.setShadowLayer(0, 0, 0, 0);
        toast.show();
        return toast;
    }
    
    /* a toast with style by default (black background and white text, ...) */
    public static Toast showToast(Context context, CharSequence text) {
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.setGravity(Gravity.CENTER, 0, 0);
        LinearLayout toastLayout = (LinearLayout) toast.getView();
        toastLayout.setBackgroundResource(R.drawable.background_round_black);
        TextView toastTV = (TextView) toastLayout.getChildAt(0);
        toastTV.setTextSize(40);
        toastTV.setTypeface(null, Typeface.BOLD);
        toast.show();
        return toast;
    }

如果使用白色背景,默认的 Toast 效果是会给字体加阴影的,但在白色背景下非常难看。默认的黑色背景加白色带阴影字体。所以用白色背景时,需要去掉阴影。

 

Toast 显示时候会一个接一个显示,所以可能会造成延时。所以显示后一个 Toast 时应该把前面的取消。做法就是调用 cancel() 方法。

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。