- 浏览: 560821 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (338)
- 已过时文章(留念用) (39)
- Android学习笔记 (30)
- Android开发指引自译 (100)
- Android NDK文档自译 (23)
- Android设计指引自译 (2)
- xp(ペケピー)&linux(理奈、铃)酱~ (4)
- ui酱&歌词自译~ (9)
- lua酱~ (9)
- 自我反省 (1)
- 羽game计划 (1)
- XSL酱 (2)
- java酱 (3)
- 设计的领悟 (58)
- 涂鸦作品(pixiv) (1)
- ruby酱 (2)
- Objective-C编程语言自译 (2)
- Android开发月报 (6)
- objc酱 (2)
- photoshop (3)
- js酱 (6)
- cpp酱 (8)
- antlr酱 (7)
- Lua 5.1参考手册自译 (11)
- 收藏品 (3)
- 待宵草计划 (4)
- 体验版截图 (1)
最新评论
-
naruto60:
太给力了!!!!我这网打不开Intel官网,多亏楼主贴了连接, ...
使用HAXM加速的Android x86模拟器(和一些问题) -
yangyile2011:
谢谢博主,翻译得很好哦
【翻译】(4)片段 -
ggwang:
牙痛的彼岸:痹!
牙痛的彼岸 -
ggwang:
总结得很简练清晰啊,学习了!
ANTLR学习笔记一:概念理解 -
leisurelife1990:
mk sdd
用git下载Android自带app的源代码
【翻译】(19)Toast通知
see
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
原文见
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
-------------------------------
Toast Notifications
Toast通知
-------------------------------
Quickview
快速概览
A toast is a message that appears on the surface of the screen for a moment, but it does not take focus (or pause the current activity), so it cannot accept user input
一个Toast是一种出现在屏幕表面上一阵子的消息,但它不获取焦点(或暂停当前活动),所以它不能接受用户输入
You can customize the toast layout to include images
你可以定制Toast布局以包含图片
In this document
本文目录
* The Basics 基础
* Positioning your Toast 放置你的Toast
* Creating a Custom Toast View 创建一个自定义Toast视图
Key classes
关键类
Toast
-------------------------------
A toast notification is a message that pops up on the surface of the window. It only fills the amount of space required for the message and the user's current activity remains visible and interactive. The notification automatically fades in and out, and does not accept interaction events.
一个Toast通知是一种消息,在窗口的表面上弹出。它只填充消息所需数量的空间,而用户的当前活动保持可见和可交互。通知自动地淡进和淡出,并且不接受交互事件。
The screenshot below shows an example toast notification from the Alarm application. Once an alarm is turned on, a toast is displayed to assure you that the alarm was set.
下面的截屏展示一个来自闹钟应用程序的示例Toast通知。一旦一个闹钟被打开,Toast被显示以向你确保闹钟被设置。
(图略:
这个闹钟被设置在从现在开始17小时又57分钟后。
)
A toast can be created and displayed from an Activity or Service. If you create a toast notification from a Service, it appears in front of the Activity currently in focus.
Toast可以从Activity或Service中被创建和显示。如果你从一个Service中创建一个Toast通知,那么它出现在当前焦点下的Activity的前方。
If user response to the notification is required, consider using a Status Bar Notification.
如果需要用户响应通知,请考虑使用状态栏通知。
-------------------------------
The Basics
基础
First, instantiate a Toast object with one of the makeText() methods. This method takes three parameters: the application Context, the text message, and the duration for the toast. It returns a properly initialized Toast object. You can display the toast notification with show(), as shown in the following example:
首先,用其中一种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();
-------------------------------
This example demonstrates everything you need for most toast notifications. You should rarely need anything else. You may, however, want to position the toast differently or even use your own layout instead of a simple text message. The following sections describe how you can do these things.
这个示例演示你对大多数Toast通知所需的任何东西。你应该极少地需要其它任何东西。然而,你可能希望不一样地放置Toast甚至或者使用你自己的布局而非简单的文本消息。以下章节描述你如何做这些事情。
You can also chain your methods and avoid holding on to the Toast object, like this:
你还可以链接你的方法并避免一直持有Toast对象,像这样:
-------------------------------
Toast.makeText(context, text, duration).show();
-------------------------------
-------------------------------
Positioning your Toast
放置你的Toast
A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.
一个标准Toast通知出现在屏幕底部附近,水平地居中。你可以用setGravity(int, int, int)方法改变这个位置。它接受三个参数:一个Gravity常量,一个x位置偏移,以及一个y位置偏移。
For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this:
例如,如果你决定Toast应该出现在左上角,你可以像这样设置重力:(注:重力方向是指淡进的方向?)
-------------------------------
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
-------------------------------
If you want to nudge the position to the right, increase the value of the second parameter. To nudge it down, increase the value of the last parameter.
如果你希望把位置微移到右边,请增加第二参数的值。如果想把它向下微移,请增加最后参数的值。
Creating a Custom Toast View
创建一个自定义Toast视图
If a simple text message isn't enough, you can create a customized layout for your toast notification. To create a custom layout, define a View layout, in XML or in your application code, and pass the root View object to the setView(View) method.
如果一个简单文本消息不足够,你可以为你的Toast通知创建一个定制的布局。为了创建一个定制布局,在XML或在你的应用程序代码中定义一个View布局,并传递根View对象给setView(View)方法。
For example, you can create the layout for the toast visible in the screenshot to the right with the following XML (saved as toast_layout.xml):
例如,你可以用以下的XML(存储为toast_layout.xml)为Toast创建右边截屏中所示的布局:
(图略:
你好!这是一个自定义Toast!
)
-------------------------------
<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>
-------------------------------
Notice that the ID of the LinearLayout element is "toast_layout". You must use this ID to inflate the layout from the XML, as shown here:
注意LinearLayout元素的ID是toast_layout。你必须使用这个ID从XML中解压布局,正如这里显示的那样:
-------------------------------
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();
-------------------------------
First, retrieve the LayoutInflater with getLayoutInflater() (or getSystemService()), and then inflate the layout from XML using inflate(int, ViewGroup). The first parameter is the layout resource ID and the second is the root View. You can use this inflated layout to find more View objects in the layout, so now capture and define the content for the ImageView and TextView elements. Finally, create a new Toast with Toast(Context) and set some properties of the toast, such as the gravity and duration. Then call setView(View) and pass it the inflated layout. You can now display the toast with your custom layout by calling show().
首先,用getLayoutInflater()(或getSystemService())获取LayoutInflater,然后从XML中使用inflate(int, ViewGroup)解压布局。第一参数是布局资源ID,而第二参数是根View。你可以使用这个解压布局在布局中查找更多View对象,所以现在可以捕获并定义ImageView和TextView元素的内容。最后,用Toast(Context)创建一个新的Toast并设置Toast的一些属性,诸如重力和持续时间。然后调用setView(View)并传给它解压布局。你现在可以通过调用show()显示带有你自定义布局的Toast。
-------------------------------
Note: Do not use the public constructor for a Toast unless you are going to define the layout with setView(View). If you do not have a custom layout to use, you must use makeText(Context, int, int) to create the Toast.
注意:不为一个Toast使用public构造函数,除非你打算用setView(View)定义布局。如果你没有一个要使用的自定义布局,你必须使用makeText(Context, int, int)创建Toast。
-------------------------------
Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.
除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。
Android 4.0 r1 - 01 Dec 2011 20:53
-------------------------------
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)
发表评论
-
【翻译】(9-补丁2)电话簿提供者
2012-07-18 12:54 2335【翻译】(9-补丁2)电话簿提供者 see h ... -
【翻译】(8-补丁1)Android接口定义语言(AIDL)
2012-07-02 05:55 2855【翻译】(8-补丁1)Andro ... -
【翻译】(0)应用组件
2012-06-30 23:50 790【翻译】(0)应用组件 see http:// ... -
【翻译】(88)传感器
2012-05-21 21:25 1026【翻译】(88)传感器 ... -
【翻译】(87)复制与粘贴
2012-05-20 14:48 1887【翻译】(87)复制与粘贴 see http: ... -
【翻译】(86)音频捕捉
2012-05-16 15:14 1041【翻译】(86)音频捕捉 ... -
【翻译】(85)照相机
2012-05-13 15:09 3731【翻译】(85)照相机 see http:// ... -
【翻译】(84)JetPlayer
2012-04-21 16:24 938【翻译】(84)JetPlayer see h ... -
【翻译】(83)媒体回放
2012-04-21 16:00 1802【翻译】(83)媒体回放 see http:/ ... -
【翻译】(82)多媒体与照相机
2012-04-18 23:05 906【翻译】(82)多媒体与照相机 see htt ... -
【翻译】(23-补丁3)构建无障碍服务
2012-04-18 21:57 1585【翻译】(23-补丁3)构 ... -
【翻译】(23-补丁2)使应用程序无障碍
2012-04-16 13:08 2032【翻译】(23-补丁2)使应用程序无障碍 see ... -
【翻译】(23-补丁1)无障碍
2012-04-11 22:38 859【翻译】(23-补丁1)无 ... -
【翻译】(81)Renderscript之运行时API参考手册
2012-04-11 22:13 1380【翻译】(81)Renderscript之运行时API参 ... -
【翻译】(80)Renderscript之计算
2012-04-09 14:09 1393【翻译】(80)Renderscript之计算 ... -
【翻译】(79)Renderscript之图形
2012-04-08 13:59 2803【翻译】(79)Renderscript之图形 ... -
【翻译】(78)Renderscript
2012-04-04 15:35 1395【翻译】(78)Renderscript see ... -
【翻译】(77)可绘画对象动画
2012-03-18 10:52 667【翻译】(77)可绘画对象动画 see htt ... -
【翻译】(76)视图动画
2012-03-18 10:04 762【翻译】(76)视图动画 see http:/ ... -
【翻译】(75)属性动画
2012-03-17 18:24 2477【翻译】(75)属性动画 see http:/ ...
相关推荐
易语言仿Win10弹出Toast通知源码,仿Win10弹出Toast通知,WndProc,LOWORD,HIWORD,Gdiplus_初始化,Init_SUITypedef_NineGrids,P_SUITypedef_NineGrids,SUIRGBtoARGB,SUIDrawGraphicNineGrids,SUIDrawGraphicAlpha,RPM,...
易语言源码易语言Toast通知源码.rar
易语言Toast通知源码。@易语言资源网站。
Toast通知.rar
易语言Toast通知源码,Toast通知,Initialize,RunMessageLoop,WndProc,DisplayToast,CreateToast,CreateToastXml,SetImageSrc,SetTextValues,SetNodeValueString,L,SUCCEEDED,LOWORD,COM_ToastEventHandler,...
易语言仿Win10弹出Toast通知源码。@易语言源码分享站。
Windows-10-Toast-Notifications, python 库显示 Windows 10 Toast通知 Windows 10 Toast通知 用于显示 Windows 10 Toast通知的easy-to-use python 库,这对 Windows GUI开发非常有用。 安装pip install win
Toast通知polyfill
利用Windows10系统API实现的C#通知,可以实现使用C#推送通知到Win10通知栏,并可以和该通知进行交互。
状态栏 Toast 通知.zip,三式吐司
Toast追踪器-Toast弹出通知来源查看
最近项目中出现一个问题,就是有的手机在关闭系统通知,结果项目中使用的原生Toast在有的手机上竟然不显示了,然后就去查系统源码,发现原来原生的Toast是基于NotificaitionManagerService实现的,难怪有些手机不...
Toast通知,windows app很常用的一项技术。源码……
WinToast:WinToast是一个用C ++编写的轻量级库,它完全集成了Windows 8和Windows 10的现代Toast通知。Toast通知使您的应用可以通知用户应了解的相关信息和及时事件并对其采取行动。在您的应用程序内部,例如新的...
C++的win10发送消息通知的例子,vs程序,必须要2015以上才能运行,toast通知是用xml文件定义的,xml文件可以自己修改
主要介绍了Android Toast通知用法,集合实例形式详细分析了Toast通知的基本使用方法与常见显示布局效果实现技巧,需要的朋友可以参考下
实际上用户本意只是想关闭Notification,但是Toast的show方法中有调用INotificationManager这个类,而这个类在用户关闭消息通知权限的同时被禁用了,所以我们的吐司无法显示。 Toast.show() 效果图 自定义Toast...
toast和context 消息栏 消息提示 通知
自定义Toast,设置Toast显示位置,自定义Toast的复杂布局 博客地址:http://blog.csdn.net/xy_nyle/article/details/20137953