首页 > 代码库 > widget(3、Toast)

widget(3、Toast)

Toast是一个提示小组件,最长用方法如下:

Toast toast = new Toast(this);toast = Toast.makeText(getApplicationContext(),    "提示:输入参数错误", Toast.LENGTH_SHORT); //持续时长 toast.setGravity(Gravity.CENTER, 0, 0);//设置位置LinearLayout toastView = (LinearLayout) toast.getView();ImageView image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.ic_launcher);toastView.addView(image,
0); //增加图片toast.show();

备注:这里要注意的是Toast也是一个viewgroup,因此可以定制复杂布局的toast,如下:

LayoutInflater inflater = getLayoutInflater();  View layout = inflater.inflate(R.layout.toast, (ViewGroup)findViewById(R.id.toast)); //自定义一个toast命名的layoutEditText text1 = (EditText)layout.findViewById(R.id.editText1);//获取布局中的某一个viewtext1.setText("hello text1");        Toast toast = new Toast(getApplicationContext());  toast.setDuration(Toast.LENGTH_LONG);  toast.setView(layout);  toast.show(); 

widget(3、Toast)