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() 方法。

 

posted @ 2015-01-08 18:17  davesuen  阅读(311)  评论(0编辑  收藏  举报