文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > 计算机软件及应用 > 【Android开发API】用户界面 - Toast通知 - Toast Notifications
侵权投诉

【Android开发API】用户界面 - Toast通知 - Toast Notifications

"doc.guandang.net; "@+id/toast_layout_root" "@+"horizontal" "fill_parent""fill_parent" "10dp" "#DAAA" > android:id"green">"@+id/image" "wrap_content" "fill_parent" "10dp" />

android:id"green">"@+id/text" "wrap_content" "fill_parent" "#FFF" /> | style=" width:250px;text-align:center; " |文件:2.png |}

注意:LinearLayout元素的ID属性值是“toast_layout_root”。你必须使用这个ID的把XML的定义填充到布局(layout)中,方法如下: 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)方法把XML定义填充到布局(layout)中,这个方法的第 个参数是布局(layout)资源的ID,第 个参数是被填充的布局(layout)View对象,本例中是root View对象。你能够使用这个被填充的布局来查找更多的View对象,以便获取和定义ImageView和TextView元素的内容。最后,用Toast(Context)方法创建 个Toast对象,并设置 些Toast的属性,如Gravity常量和持续显示的时间等。然后调用setView(View)方法,把它传递给要填充的布局(layout)对象。然后调用show()方法显示自定义的Toast。

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

©eoe移动开发者社区

本文链接: doc.guandang.net

上一页第2页

你可能喜欢

  • Android开发教程
  • Android开发技术
  • Android开发规范
  • Android开发报告
  • Android开发笔记
  • Android应用开发
  • Android程序开发

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email