android toast代码,Android开发之Toast(示例代码)

本文分享了Android开发中Toast的两种使用方法:系统默认和自定义。系统默认方式简单直接,而自定义方式允许开发者设置显示位置、时间及自定义视图。在自定义Toast时,关键在于视图的引入。作者通过实例展示了如何创建自定义的Toast,并期望得到更多关于自定义视图的指导。

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

第一次在博客园发布文章,就把我刚弄明白的关于Android开发中的提示设置,分享给大家。

Toast是Android中经常用到的一个方法,用于简单的用户提示,经过摸索我发现了Toast的两种使用方式,先不扯别的,分享一下,还望大师指教。

第一种系统默认的方式:

Toast.makeText(getApplicationContext(), "系统自带提示形式", Toast.LENGTH_SHORT).show();

第二种自定义方式:

Toast toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);//设置显示的位置

toast.setDuration(Toast.LENGTH_LONG);//设置显示的时间

toast.setView(getLayoutInflater().inflate(R.layout.activity_two,null));//设置自定义的视图

toast.show();

在自定义中显示的视图:

android:id="@+id/linearLayout1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

android:background="@drawable/fire_eye_alien"

android:layout_width="50dip"

android:layout_height="50dip"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:text="自定义形式提示"

android:textSize="20dip"

/>

个人感觉对于自定义类型的Toast,难点在于视图的引入,希望大神能给出更多的指导。

c10cc401752ae91c80d93f06bd3c0529.png

68d26efc2ecbe47d3f08c373632a552c.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值