码迷,mamicode.com
首页 > 移动开发 > 详细

Android Toast

时间:2015-01-08 19:44:13      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Android Toast

标签:

原文地址:http://www.cnblogs.com/davesuen/p/4211655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!