Toasts

Toast在小弹出窗口中提供有关操作的简单反馈。 它仅填充消息所需的空间量,并且当前活动保持可见和交互。 例如,在发送电子邮件之前离开电子邮件会触发“已保存的草稿”面包,以告知您以后可以继续编辑。 超时后Toast自动消失。

 

如果需要用户对状态消息作出响应,请考虑使用通知 Notification

 

 

【基础】

首先,使用一个makeText()方法实例化一个Toast对象。 此方法需要三个参数:应用程序上下文,文本消息和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

标准Toast通知显示在屏幕底部附近,水平居中。 您可以使用setGravityintintint)方法更改此位置。 这接受三个参数:重力常数,x位置偏移和y位置偏移。

 

例如,如果你决定吐司应该出现在左上角,你可以像这样设置重力:

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

如果要向右移动位置,请增大第二个参数的值。要向下移动,请增加最后一个参数的值。

 

【创建自定义Toast视图】

如果简单的短信不够用,您可以为您的toast通知创建自定义布局。 要创建自定义布局,请以XML或应用程序代码定义视图布局,并将根View对象传递给setView(View)方法。

 

例如,您可以使用以下XML(保存为layout / custom_toast.xml)在右侧屏幕截图中为toast可见创建布局:

 

              android:id="@+id/custom_toast_container"

              android:orientation="horizontal"

              android:layout_width="fill_parent"

              android:layout_height="fill_parent"

              android:padding="8dp"

              android:background="#DAAA" >

 

android:src="@drawable/droid"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginRight="8dp" />

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#FFF"  />

 

请注意,LinearLayout元素的ID是“custom_toast_container”。 您必须使用此ID和XML布局文件“custom_toast”的ID来扩充布局,如下所示:

LayoutInflater inflater = getLayoutInflater();

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

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

 

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,第二个是根视图。 您可以使用此膨胀布局在布局中查找更多的View对象,因此现在可以捕获并定义ImageView和TextView元素的内容。 最后,使用Toast(上下文)创建一个新的Toast,并设置Toast的一些属性,如重力和持续时间。 然后调用setView(View)并传递它膨胀的布局。 现在,您可以通过调用show()来显示带有自定义布局的Toast。

 

注意:不要使用Toast的公共构造函数,除非您要使用setView(View)定义布局。 如果你没有自定义布局要使用,你必须使用makeText(Context,int,int)来创建Toast。

 

更多相关文章

  1. Android内存小谈 - Birdmafly
  2. Android(安卓)layout 使用include和merge 标签
  3. Android(安卓)性能优化 内存优化 How to do
  4. [置顶] Android(安卓)listview 部分布局监听
  5. android TextView 字体竖着写(纵向)的方案
  6. Android(安卓)Binder实列篇
  7. android在java代码中动态添加组件及相关布局方法(LayoutParams)
  8. Android(安卓)API Guides---User Interface
  9. Android生猛上手,先写个拨号器。

随机推荐

  1. Android中 ScrollView(ListView)中嵌套List
  2. Android简单自定义圆形和水平ProgressBar
  3. Android原生(Native)C(JNI/NDK)开发之二:f
  4. Android软硬整合设计与框架揭秘教程
  5. Android学习-RecyclerView默认scrollbar
  6. TextView去除内边距
  7. ubuntu 配置Android(安卓)开发环境
  8. Arcgis android 10.2安装方法
  9. Android使用ActivityGroup设置android:wi
  10. android控件布局