各式各样的Android Toast

 

在android开发中,你可能会用到各种各样的Toast,我这里封装了一些方法,可以满足你的需要,短显,长显,在中央位置长显示,在中央位置短显示,带图标的toast(也分为长显,短显,和显示位置的控制),代码如下:

package utils

import android.content.Context;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ToastUtil
{
    public static void show(Context context, CharSequence message, boolean _short)
    {
        if (_short)
        {
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
        }
    }

    public static void showShort(Context context, CharSequence message)
    {
        show(context, message, true);
    }

    public static void showLong(Context context, CharSequence message)
    {
        show(context, message, false);
    }

    public static void showAtCenter(Context context, CharSequence message, boolean _short)
    {
        Toast toast = null;
        if (_short)
        {
            toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        }
        else
        {
            toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
        }
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public static void showAtCenterShort(Context context, CharSequence message)
    {
        showAtCenter(context, message, true);
    }

    public static void showAtCenterLong(Context context, CharSequence message)
    {
        showAtCenter(context, message, false);
    }

    public static void showWithPic(Context context, int drawable, CharSequence message, boolean _short)
    {
        Toast toast = null;
        if (_short)
        {
            toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        }
        else
        {
            toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
        }
        ImageView image = new ImageView(context);
        image.setImageResource(drawable);
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setHorizontalGravity(LinearLayout.HORIZONTAL);
        linearLayout.addView(image);
        linearLayout.addView(toast.getView());
        toast.setView(linearLayout);
        toast.show();
    }

    public static void showWithPicShort(Context context, int drawable, CharSequence message)
    {
        showWithPic(context, drawable, message, true);
    }

    public static void showWithPicLong(Context context, int drawable, CharSequence message)
    {
        showWithPic(context, drawable, message, false);
    }

    public static void showWithPicAtCenter(Context context, int drawable, CharSequence message, boolean _short)
    {
        Toast toast = null;
        if (_short)
        {
            toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        }
        else
        {
            toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
        }
        toast.setGravity(Gravity.CENTER, 0, 0);
        ImageView image = new ImageView(context);
        image.setImageResource(drawable);
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setHorizontalGravity(LinearLayout.HORIZONTAL);
        linearLayout.addView(image);
        linearLayout.addView(toast.getView());
        toast.setView(linearLayout);
        toast.show();
    }

    public static void showWithPicAtCenterShort(Context context, int drawable, CharSequence message)
    {
        showWithPicAtCenter(context, drawable, message, true);
    }

    public static void showWithPicAtCenterLong(Context context, int drawable, CharSequence message)
    {
        showWithPicAtCenter(context, drawable, message, false);
    }
}

 

posted @ 2012-09-04 11:12  yangann  阅读(260)  评论(0编辑  收藏  举报