Toast自定义自己的布局

本文详细介绍了Android平台上的Toast通知,包括其基本概念、显示方式及如何自定义布局等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Toast通知是一条弹出显示在窗口表面的消息,它只占据足够显示消息内容的屏幕空间,并且用户当前的activity仍然保持可见和可操作。这个通知自动淡入淡出,并不接收交互事件。
Toast是在屏幕表面显示片刻的一条消息,它不会抢占用户焦点(或者暂停当前的activity),所以它不接收用户输入
你可以自定义Toast的布局layout,使其包含图片

基础


首先,用makeText()方法实例化一个Toast对象。该方法需要三个参数:当前应用的Context,文本消息,和toast的持续时间。该方法返回一个实例化过的Toast对象。你可以用show()方法将该toast通知显示出来,见下例:

Context context = getApplicationContext();
CharSequence text = "Hello toast!"
intduration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

这个例子演示大部分你使用toast通知所需要的,你很少会需要其他的。有可能你会想要把toast放置在其他位置或者使用你自己的布局来代替默认的简单文本消息布局。下一节描述了如何使用它们。

你也可以链接调用方法已避免保留一个Toast对象,例如:

Toast.makeText(context, text, duration).show();

定位你的toast


一个标准toast通知垂直剧中出现在靠近屏幕底部的位置。你可以通过setGravity(int, int, int)方法来改变其位置。三个参数分别是:一个Gravity常量,一个x方向的偏移值和一个y方向的偏移值。

例如,如果你决定让toast出现在左上角,你可以这样设置:

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

如果你想要向右移动,增加第二个参数的值;增加第三个参数的值向下移动。

创建自定义的Toast视图


如果一个简单的文本消息已经无法满足你的需求,你可以自己定义一个toast通知的布局layout。在XML或者代码中定义一个View的布局,并根View对象传递给setView(View)方法。

例如,你可以用如下所示XML创建右图中的toast通知的布局(保存为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="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:layout_textColor="#FFF"/>
</LinearLayout> 

注意LinearLayout元素的ID是“toast_layout”。你必须使用这个ID从XML中展开(inflate)布局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展开(inflate)布局layout。第一个参数是layout资源ID,第二个参数是根View。你可以使用这个布局来查找其内部更多的View对象。因此获得和定义ImageView和TextView元素的内容。最后,用Toast(Context)创建一个新的toast并设置属性,例如重心和持续时间。然后调用setView(View)并传入布局对象,你就可以通过show()来显示自定义布局的toast了。

http://www.android-doc.com/guide/topics/ui/notifiers/toasts.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值