自定义toast

来源:互联网 发布:人工智能李开复百度云 编辑:程序博客网 时间:2024/06/11 01:22

一些时候需要用到自定义的toast,

从源码的角度分析下:

看他的makeText方法:

/**
     * Make a standard toast that just contains a text view.
     *
     * @param context  The context to use.  Usually your {@link android.app.Application}
     *                 or {@link android.app.Activity} object.
     * @param text     The text to show.  Can be formatted text.
     * @param duration How long to display the message.  Either {@link #LENGTH_SHORT} or
     *                 {@link #LENGTH_LONG}
     *
     */
    public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
        Toast result = new Toast(context);


        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
        TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
        tv.setText(text);
        
        result.mNextView = v;
        result.mDuration = duration;


        return result;
    }


首先:他new 了一个toast,然后渲染了一个系统的布局进来,result.mNextView = v; 设置view,result.mDuration = duration;再设置时间,最后再调用show方法即可,

然后我们就阔以模拟他自定义toast了

原创粉丝点击