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命名的layout

EditText text1 = (EditText)layout.findViewById(R.id.editText1);//获取布局中的某一个view

text1.setText("hello text1");        

Toast toast = new Toast(getApplicationContext());  

toast.setDuration(Toast.LENGTH_LONG);  

toast.setView(layout);  

toast.show(); 

你可能感兴趣的:(widget)