最新消息:欢迎访问Android开发中文站!商务联系微信:loading_in

常见的Toast效果

源码下载 AndroidChina 4545浏览 0评论

Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。

1.默认效果:

03_toast

代码:


@Override
Toast.makeText(this, TOASTBTN_1, Toast.LENGTH_LONG).show();

2.自定义显示位置效果:

03_toast2

代码:

 toast = Toast.makeText(getApplicationContext(), TOASTBTN_2,
 Toast.LENGTH_LONG);
 toast.setGravity(Gravity.CENTER, 0, 0);
 toast.show();

3.带图片效果:

03_toast3

代码:

 toast = Toast.makeText(getApplicationContext(), TOASTBTN_3, Toast.LENGTH_LONG);
 toast.setGravity(Gravity.CENTER, 50, -100);
 LinearLayout layout = (LinearLayout) toast.getView();
 ImageView image = new ImageView(getApplicationContext());
 image.setImageResource(R.drawable.wallpaper_tree_small);
 layout.addView(image, 0);
 toast.show();

4.完全自定义效果:

03_toast4
代码:

 LayoutInflater inflater = getLayoutInflater();
 View view = inflater.inflate(R.layout.userdefinedtoast, (ViewGroup) findViewById(R.id.toast_layout));
 toast = new Toast(getApplicationContext());
 toast.setGravity(Gravity.CENTER, 0, 0);
 toast.setDuration(Toast.LENGTH_LONG);
 toast.setView(view);
 toast.show();

5.长时间显示效果:

与自定义效果一样,只是不会自动消失,本质上是一个dialog。
03_toast4
代码:

 LayoutInflater inflater1 = getLayoutInflater();
 View view1 = inflater1.inflate(R.layout.userdefinedtoast,
 (ViewGroup) findViewById(R.id.toast_layout));
 builder = new AlertDialog.Builder(this);
 builder.setView(view1);
 dialog = builder.create();
 dialog.show();

源码下载:点击下载

转载请注明:Android开发中文站 » 常见的Toast效果

您必须 登录 才能发表评论!