自定义 Toast View

来源:互联网 发布:芒种日期算法 编辑:程序博客网 时间:2024/06/14 08:12

1.获得Toast view 添加view

Toast toast = Toast.makeText(MainActivity.this, "自定义View", Toast.LENGTH_SHORT);                LinearLayout linearLayout = (LinearLayout) toast.getView();                ImageView image = new ImageView(MainActivity.this);                image.setImageResource(R.mipmap.ic_launcher);                linearLayout.addView(image);                toast.show();
Toast.makeText源码:
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;    }

2.自定义Toast view

Toast toast=new Toast(context);                View viewMain = LayoutInflater.from(context).inflate(R.layout.activity_main, null);                toast.setView(viewMain);                toast.show();


0 0
原创粉丝点击