Toasts(提示框)
来源:互联网 发布:知微数据招聘 编辑:程序博客网 时间:2024/05/17 23:31
提示框
提示框可以在一个小的弹出框里提供关于操作的简单反馈。它只会占用消息所需要的空间并且当前的activity始终是可见可交互的。例如,在你发送邮件前离开时会触发一个“保存为草稿”的提示框让你知道你可以以后继续编辑。提示框会在一段时间后自动消失。
如果需要用户响应这个状态信息的话,请考虑使用 Notification 来代替。
基础知识
首先使用 makeText() 方法实例化一个 Toast
对象。该方法需要三个参数:应用 Context,文本消息和提示框持续时长。它会返回一个恰当初始化的Toast对象。你可以使用 show() 方法显示提示框通知,就像下面的实例中演示的那样:
Context context = getApplicationContext();CharSequence text = "Hello toast!";int duration = Toast.LENGTH_SHORT;Toast toast = Toast.makeText(context, text, duration);toast.show();
对于大部分提示框通知来说这个实例演示了所有你需要的东西。你应该不会需要其他东西了。然而,你可能想要提示框的放置的位置有所不同,又或者你不想使用简单的文本消息而是使用自己的布局。下面的章节会向你介绍如何达到这些效果。
你也可以把你的方法串起来,避免持有Toast对象,就像这样:
Toast.makeText(context, text, duration).show();
放置你的提示框
标准的提示框通知出现在靠近屏幕底部的位置并且水平居中。你可以使用 setGravity(int, int, int) 方法来修改位置。它接收三个参数:Gravity 常量,x坐标偏移量,y坐标偏移量。
例如,如果你决定提示框应该出现在左上角,你可以像这样设置gravity:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
如果你想要把位置向右边移动,增加第二个参数的数值即可。要想往下移动,增加最后一个参数的数值。
创建自定义提示框视图
如果简单文本消息不够用的话,你可以为你的提示框通知创建自定义的布局。在XML或应用的代码里定义一个视图布局,然后把根 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="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>
注意线性布局元素的ID是“toast_layout_root”。你必须使用这个ID从XML中扩充布局,就像这样:
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) 从XML中扩充布局。第一个参数是布局资源的ID,第二个参数是根视图。你可以使用这个扩充布局来获取布局中更多的视图对象,所以现在捕获并定义ImageView和TextView元素的内容。最终,使用 Toast(Context) 创建一个新的Toast,再设置诸如gravity和持续时长的参数,然后把扩充的布局传入 setView(View) 中并调用。现在你可以调用 show() 来显示自定义布局的提示框了。
注解:除非你准备使用 setView(View) 定义布局,否则不要使用公有的构造方法创建提示框。如果你不需要使用自定义布局,你应该使用 makeText(Context, int, int)
去创建提示框。
- Toasts(提示框)
- Toasts
- Toasts
- Hust oj 1384 Leyni, LOLI and Toasts(水题)
- android Toasts
- android - Toasts
- 【3】Toasts
- 自定义Android Toasts风格
- 自定义Android Toasts风格
- Android API Guides---Toasts
- API Guides——Toasts
- 时间、日期、Toasts和进度条Dialog
- UIAlertView(提示框)
- 提示框(一)
- 加载动画、提示框动态添加(普通提示框、确认提示框、消息提示框)
- Android开发总结笔记 Toasts(吐司泡) 1-1-17
- Android UI系列-----时间、日期、Toasts和进度条Dialog
- Jquery提示框效果(确认提示框)
- 陪着时光,走过羊肠阡陌
- android view.layout(l,t,r,b) 不更新问题
- 相称显微镜下细胞群体跟踪
- 实现图片和UIView的多点触摸缩放
- Ice异步程序设计----AMI,AMD
- Toasts(提示框)
- C#程序集
- ios 地图大头针自定义显示图片 MKAnnotationView
- [LeetCode]Jump Game II
- mysql5.1数据库升级
- 读书笔记:关于相机标定和三维重建
- 根据经纬度获得城市
- 建立基于MyEclipse 6和XFire开发的Web Service应用的客户端测试程序
- vs2010+open244的永久性配置