Android:自定义toast

效果图:

Android:自定义toast_第1张图片

代码:

//自定义布局的toast
        customViewToast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(ToastTest.this, "top", Toast.LENGTH_SHORT);

                //此处只使用一个TextView,当然也可以使用更复杂的View
                TextView tv = new TextView(ToastTest.this);
                tv.setText("CustomView toast.");
                tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
                tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.abc_ic_menu_share_mtrl_alpha, 0, 0, 0);
                tv.setBackgroundColor(Color.parseColor("#ff88ff"));
                tv.setPadding(30, 10, 30, 10);
                tv.setGravity(Gravity.CENTER_VERTICAL);

                toast.setView(tv);
                toast.show();
            }
        });


你可能感兴趣的:(android)