Android Toast和Notification
1. Toast用法
Toast 可以设置:时间,位置,自定义View
1.1 最普通的Toast
Toast.makeText(ToastActivity.this, "CarloZ Short Toast", Toast.LENGTH_SHORT).show();
1.2 改变Toast的显示位置
Toast shortToast = Toast.makeText(ToastActivity.this, "CarloZ Short Toast", Toast.LENGTH_SHORT); shortToast.setGravity(Gravity.CENTER, 0, 0); // 显示在屏幕上的位置 shortToast.show();
1.3 显示自定义View
Toast imgToast = Toast.makeText(ToastActivity.this, "CarloZ Image Toast", Toast.LENGTH_SHORT); ImageView iv = new ImageView(ToastActivity.this); iv.setImageResource(R.drawable.ic_launcher); TextView tv = new TextView(ToastActivity.this); tv.setText("CarloZ Image Toast"); tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));; LinearLayout ll = new LinearLayout(ToastActivity.this); ll.setOrientation(LinearLayout.VERTICAL); ll.addView(tv); ll.addView(iv); //imgToast.setView(iv); //图片代替了文本,如果要显示多个元素,要自定义layout,其中包含多个元素 imgToast.setView(ll); imgToast.show();
效果如下
2. Notification使用