Android开发指南(35) —— Toast Notifications

 

前言

  本章内容为Android开发者指南的 Framework Topics/User Interface/Notifications/Toast Notifications章节,译为"媒体播放",版本为Android 4.0 r1,翻译来自:"呆呆大虾",欢迎访问他的微博:"http://weibo.com/popapa",再次感谢"呆呆大虾" !期待你一起参与翻译Android的相关资料,联系我over140@gmail.com。

 

声明

  欢迎转载,但请保留文章原始出处:) 

    博客园:http://www.cnblogs.com/

    Android中文翻译组:http://androidbox.sinaapp.com/

 

 

 

Toast通知

译者署名: 呆呆大虾

译者微博:http://weibo.com/popapa

版本:Android 4.0 r1

 

声明

       本文整理自原作者:http://leybreeze.com/?p=461

 

原文

       http://developer.android.com/guide/topics/ui/notifiers/toasts.html

 

快速查看

Toast是一种只在屏幕上显示一小会儿的消息,它没有焦点(也不暂停当前的activity),因此也不能接受用户的输入。

可以通过定制toast的布局来显示图片

 

在本文中:

基础

定位Toast

创建自定Toast视图

 

关键类

Toast

 

toast通知是一种在窗口表面弹出的消息。它只占用信息显示所需的空间,用户当前的activity仍保持可见并可交互。该通知自动实现淡入淡出,且不接受人机交互事件。

以下截图展示了闹钟程序的toast通知示例。一旦闹钟被打开,就会显示一条toast作为对设置的确认。

toast能被Activity Service创建并显示。如果由Service创建,则toast会显示在当前已获得焦点的Activity前面。

如果需要用户对通知进行响应,可以考虑使用Status Bar Notification

 

 

基础知识

首先,用某个makeText()方法来实例化一个Toast对象。该方法有三个参数:应用程序上下文Context、文本信息和toast的持续显示时间。它将返回一个已正确初始化的Toast对象。可以用show()方法来显示该toast通知,示例如下:

Context context = getApplicationContext(); 

CharSequence text = "Hello toast!"; 

int duration = Toast.LENGTH_SHORT; 

Toast toast = Toast.makeText(context, text, duration); 

toast.show();

上例演示了大部分toast通知需要的所有内容,应该不大会需要用到其他内容了。不过,你也许想在其他位置显示toast或是要用自己的布局替换默认相对简单的文本消息,下一节将描述如何完成。

还可以将多个方法链接起来写,以避免持久化Toast对象,就像这样:

Toast.makeText(context, text, duration).show();

 

 

定位Toast

标准的toast通知左右居中地显示在屏幕底部附近。可以通过setGravity(int, int, int)方法来改变显示位置。它接受三个参数:重力常量常数GravityX方向偏移和Y方向偏移。

例如,如果决定把toast置于左上角,可以这样设置重力常数:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

如果想让位置向右移,就增加第二个参数的值;要向下移,就增加最后一个参数的值。

 

 

创建自定义的Toast视图

如果不满足于简单的文本消息,还可以为toast通知创建一个自定义布局。要创建自定义布局,需要用XML或程序代码定义一个View布局,然后把根View对象传给setView(View)方法。

例如,可以用以下的XML(保存为toast_layout.xml)创建出右边截图中所示的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

              android:id="@+id/toast_layout_root" 

              android:orientation="horizontal" 

              android:layout_width="fill_parent" 

              android:layout_height="fill_parent" 

              android:padding="10dp" 

              android:background="#DAAA" 

              > 

    <ImageView android:id="@+id/image" 

               android:layout_width="wrap_content" 

               android:layout_height="fill_parent" 

               android:layout_marginRight="10dp" 

               /> 

    <TextView android:id="@+id/text" 

              android:layout_width="wrap_content" 

              android:layout_height="fill_parent" 

              android:textColor="#FFF" 

              /> 

</LinearLayout>

 

注意,LinearLayout元素的ID“toast_layout”。必须用这个IDXML中解析出布局,如下:

LayoutInflater inflater = getLayoutInflater(); 

View layout = inflater.inflate(R.layout.toast_layout, 

                               (ViewGroup) findViewById(R.id.toast_layout_root)); 

 

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

image.setImageResource(R.drawable.android); 

TextView text = (TextView) layout.findViewById(R.id.text); 

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

 

Toast toast = new Toast(getApplicationContext()); 

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

toast.setDuration(Toast.LENGTH_LONG); 

toast.setView(layout); 

toast.show();

首先,用getLayoutInflater()(或getSystemService()来读取LayoutInflater,然后用inflate(int, ViewGroup)将布局(layout)从XML中解析出来。第一个参数是layout资源ID,第二个参数是根View。可以用解析出来的layout获取其他View对象,之后获取并定义ImageViewTextView元素的内容。最后,用Toast(Context)创建一个新的toast,设置一些属性如gravityduration等。然后调用setView(View)并将解析出的layout传入。现在就可以调用show()来显示自定义布局的toast了。

注意除非想用setView(View)来定义布局,否则不要用公共构造方法来构造Toast。如果没有可用的自定义布局,则必须使用makeText(Context, int, int)来创建Toast

 

posted @ 2011-11-23 09:43  农民伯伯  阅读(1551)  评论(0编辑  收藏  举报