您的位置:首页 > Web前端 > CSS

自定义简单的Toast,可自行修改文字、图片、样式

2016-05-27 10:41 609 查看
/**

     * 

     * @param context

     * @param text 直接传字符串

     * @param resId 图片ID

     */

    public static void showTwo(Context context, String text,int resId) {

        LayoutInflater inflater = LayoutInflater.from(context);

        View layout = inflater.inflate(R.layout.toast_layout_two, null);

        toast_image = (ImageView) layout.findViewById(R.id.toast_image);

      //图片可以自己从外面传递进来换;

        toast_image.setBackgroundResource(resId);

        textV = (TextView) layout.findViewById(R.id.toast_text);

        textV.setText(text);

        if (mToast == null) {

        mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);

        mToast.setGravity(Gravity.CENTER, 0, 0);

        mToast.setView(layout);

        } else {

        mToast.cancel();//关闭吐司显示

        mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);

        //设置Toast位置的,如若不设置将会是系统默认Toast位置

        mToast.setGravity(Gravity.CENTER, 0, 0);

        mToast.setView(layout);

        }

        mToast.show();

    }

    /**

     * 传字段ID

     * @param context

     * @param text 文字ID

     * @param resId 图片ID

     */

    public static void showTwo(Context context, int text,int resId) {

        LayoutInflater inflater = LayoutInflater.from(context);

        View layout = inflater.inflate(R.layout.toast_layout_two, null);

        toast_image = (ImageView) layout.findViewById(R.id.toast_image);

        //图片可以自己从外面传递进来换;

          toast_image.setBackgroundResource(resId);

        TextView textV = (TextView) layout.findViewById(R.id.toast_text);

        textV.setText(text);

        if (mToast == null) {

        mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);

        mToast.setGravity(Gravity.CENTER, 0, 0);

        mToast.setView(layout);

        } else {

        mToast.cancel();//关闭吐司显示

        mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);

        //设置Toast位置的

        mToast.setGravity(Gravity.CENTER, 0, 0);

        mToast.setView(layout);

        }

        mToast.show();
    }





源码下载地址:(初次发文,多多关注)
http://download.csdn.net/detail/qq_31850557/9532987
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: