Android编程之Toast官方使用说明译文
来源:互联网 发布:单片机延时程序原理 编辑:程序博客网 时间:2024/05/16 11:31
以下来自android官方Toast使用说明的译文
toast是一种简单到弹出反馈操作。它只占用了消息所需要的空间大小,并在当前activity显示和互动。例如,当你退出正在编写email之前,会提示一个“草稿已保存”的toast来告知你可以稍后继续编辑。Toast会在一段时间后自动消失。
首先,通过Toast中的makeText()方法创建一个Toast对象。这个方法有三个参数:Context,消息文字,显示的时间长短。然后,通过show()让其显示出来:
Context context = getApplicationContext();CharSequence text = "Hello toast!";int duration = Toast.LENGTH_SHORT;Toast toast = Toast.makeText(context, text, duration);toast.show();
标准的toast会出现在屏幕底部水平中间的位置上,你也可以调用setGravity(int,int,int)改变其显示的位置。这里的三个参数,分别表示:Gravity,x方向偏移,y方向偏移。
例如:如果你定义toast显示在左上角,那么,你可以这样写:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
如果你想让其位置往右一些,你可以增加第二个参数的值,同理,向下调时,增加最后一个参数的值。
如果这样一个简单的消息不能让你满意,你还可以创建一个自己定义的布局。需要定义一个视图,可以是xml,也可以是代码的,然后调用setView(View)设置布局。
例如:
<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="8dp" android:background="#DAAA" > <ImageView android:src="@drawable/droid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" /></LinearLayout>
代码:
LayoutInflater inflater = getLayoutInflater();View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_layout_root));TextView text = (TextView) layout.findViewById(R.id.text);text.setText("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的ID,第二个是根视图。你可以使用填充布局得到更多的内部视图,得到例如ImageView和TextView元素。最后,通过Toast(Context)创建一个对象,再设置显示位置、显示时间长短,然后调用setView(View)。现在,你可以调用show()让自定义的布局样式显示出来。
注意:不要使用公开的构造方法,除非你需要自定义视图。如果不使用特别布局的话,你应该使用makeText(Context,int,int)来创建Toast。
- Android编程之Toast官方使用说明译文
- Android开发者指南-Toast Notifications[原创译文]
- Dagger 官方文档之Dagger1(译文)
- 学习android编程之路(5)— Toast使用方法
- Android基本之Toast
- Android之Toast
- Android学习之Toast
- Android之自定义Toast
- android 界面之Toast
- Android初学者之Toast
- Android之Toast
- android之【Toast】
- Android开发之Toast
- Android之Toast使用
- Android之Toast
- Android之Toast
- Android 开发之 Toast
- Android--通知之Toast
- LA3027之并查集
- ztree 实现树的搜索功能
- 申请google map 的android key(新的申请方式)
- 信息图]乔布斯的10大管理诫律
- textarea 滚动条属性设置
- Android编程之Toast官方使用说明译文
- ORA-01078 & LRM-00109 解决方案
- linux下为已经编译好的php环境添加mysql扩展
- "Firefox is already running but is not responding" error message - How to fix it
- 问题Non-ASCII character '\xe5' in file的解决办法
- poi操作excel,复制sheet,复制行,复制单元格
- SDL1.3支持多个窗口
- ZOJ 1698 Easier Done Than Said?
- CSS瀑布流页面不规则布局