Android开发Toast Notifications

Android开发Toast Notifications 
关键类 
Toast 
toast通知是一种在窗口表面弹出的消息。它只占用信息显示所需的空间,用户当前的activity仍保持可见并可交互。该通知自动实现淡入淡出,且不接受人机交互事件。 
以下截图展示了闹钟程序的toast通知示例。一旦闹钟被打开,就会显示一条toast作为对设置的确认。 
toast能被Activity 或Service创建并显示。如果由Service创建,则toast会显示在当前已获得焦点的Activity前面。 
如果需要用户对通知进行响应,可以考虑使用Status Bar Notification。 
基础知识 
首先,用某个makeText()方法来实例化一个Toast对象。该方法有三个参数:应用程序上下文Context、文本信息和toast的持续显示时间。它将返回一个已正确初始化的Toast对象。可以用show()方法来显示该toast通知,示例如下: 
1. Context context = getApplicationContext(); 

2. CharSequence text = "Hello toast!"; 

3. int duration = Toast.LENGTH_SHORT; 
4. Toast toast = Toast.makeText(context, text, duration); 

5. toast.show(); 

复制代码 
上例演示了大部分toast通知需要的所有内容,应该不大会需要用到其他内容了。不过,你也许想在其他位置显示toast或是要用自己的布局替换默认相对简单的文本消息,下一节将描述如何完成。 
还可以将多个方法链接起来写,以避免持久化Toast对象,就像这样: 
1. Toast.makeText(context, text, duration).show(); 
复制代码 
定位Toast 
标准的toast通知左右居中地显示在屏幕底部附近。可以通过setGravity(int, int, int)方法来改变显示位置。它接受三个参数:重力常量常数Gravity,X方向偏移和Y方向偏移。 
例如,如果决定把toast置于左上角,可以这样设置重力常数: 
1. toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0); 
复制代码 
如果想让位置向右移,就增加第二个参数的值;要向下移,就增加最后一个参数的值。 
如果不满足于简单的文本消息,还可以为toast通知创建一个自定义布局。要创建自定义布局,需要用XML或程序代码定义一个View布局,然后把根View对象传给setView(View)方法。 
例如,可以用以下的XML(保存为toast_layout.xml)创建出右边截图中所示的布局: 
1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 

2.               android:id="@+id/toast_layout_root" 

3.               android:orientation="horizontal" 

4.               android:layout_width="fill_parent" 

5.               android:layout_height="fill_parent" 

6.               android:padding="10dp" 

7.               android:background="#DAAA" 

8.               > 

9.     <imageview android:id="@+id/image" 
10.                android:layout_width="wrap_content" 

11.                android:layout_height="fill_parent" 

12.                android:layout_marginRight="10dp" 

13.                /> 
14.     <textview android:id="@+id/text" 
15.               android:layout_width="wrap_content" 

16.               android:layout_height="fill_parent" 

17.               android:textColor="#FFF" 

18.               /> 
复制代码 
注意,LinearLayout元素的ID是“toast_layout”。必须用这个ID从XML中解析出布局,如下: 
1. LayoutInflater inflater = getLayoutInflater(); 

2. View layout = inflater.inflate(R.layout.toast_layout, 
3.                                (ViewGroup) findViewById(R.id.toast_layout_root)); 

4.   
5. ImageView image = (ImageView) layout.findViewById(R.id.image); 

6. image.setImageResource(R.drawable.android); 
7. TextView text = (TextView) layout.findViewById(R.id.text); 

8. text.setText("Hello! This is a custom toast!"); 

9.   
10. Toast toast = new Toast(getApplicationContext()); 

11. toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 

12. toast.setDuration(Toast.LENGTH_LONG); 

13. toast.setView(layout); 

14. toast.show() 
复制代码 
首先,用getLayoutInflater()(或getSystemService())来读取LayoutInflater,然后用inflate(int, ViewGroup)将布局(layout)从XML中解析出来。第一个参数是layout资源ID,第二个参数是根View。

可以用解析出来的layout获取其他View对象,之后获取并定义ImageView和TextView元素的内容。最后,用Toast(Context)创建一个新的toast,设置一些属性如gravity和duration等。然后调用setView(View)并将解析出的layout传入。现在就可以调用show()来显示自定义布局的toast了。 
注意:除非想用setView(View)来定义布局,否则不要用公共构造方法来构造Toast。如果没有可用的自定义布局,则必须使用makeText(Context, int, int)来创建Toast。

原文地址:https://www.cnblogs.com/coffees/p/4736729.html