Toast在使用中主要是提示作用,一般开发中我们直接使用默认的即可。但是有时候为了达到更好的视觉效果,也需要我们更改默认的Toast效果。
默认方式:
Toast.makeText(getApplicationContext(), "我是默认方式哦",Toast.LENGTH_SHORT).show();
自定义方式:
toast = Toast.makeText(getApplicationContext(), "自定义位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
toast = Toast.makeText(getApplicationContext(),<span style="font-family: Arial, Helvetica, sans-serif;">"我是有图片的Toast", Toast.LENGTH_LONG);</span>
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("我是完全自定义的Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();