基于自定义Toast全面解析
作者:HankingHu 时间:2023-08-27 19:18:55
Toast一般用来显示一行文字,用法比较固定:
Toast.makeText(Context context,String message,int duration);
但是有时候想用toast 来显示复杂的view甚至是带有图片的view时这时候就要用到自定义的Toast,自定义Toast主要用到一下几个方法如图:
1.setView()方法用来显示用户自定义的view.
2. setGravity()用来确定Toast显示的位置.
3.setDuration()用来设置Toast显示的时间长短,只有两种选择,LENGTH_SHORT,LENGTH_LONG,都是int型。
4.setText()用来显示一段文字,但是要注意的时setText()与setView(),不能同时使用不然就会出错。
下面通过一个完整的代码来看看如何实现一个自定义的Toast:
Toast_view.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tianjia_p"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
/>
</LinearLayout>
Activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myButton"
android:text="button"
/>
</LinearLayout>
ToastUtil:
public class ToastUtil {
private static Toast toast;
public static void showToast(int duration,View mview,Context context)
{
if (toast==null) {
toast = new Toast(context);
}
toast.setDuration(duration);
toast.setView(mview);
toast.show();
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private View toast_view;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater inflater=LayoutInflater.from(this);
toast_view =inflater.inflate(R.layout.toast_view,null);
button=(Button)findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtil.showToast(Toast.LENGTH_SHORT,toast_view,getApplicationContext());
}
});
}
}
上面的ToastUtil工具类 写的不好,大家可以自己改写自己的ToastUtil类。
来源:http://blog.csdn.net/u013309870/article/details/53001344
标签:自定义,Toast
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅析C#中数组,ArrayList与List对象的区别
2022-03-12 00:41:30
springboot html调用js无效400问题及解决
2023-06-24 02:11:54
![](https://img.aspxhome.com/file/2023/3/97943_0s.png)
Java静态泛型使用方法实例解析
2023-03-31 00:41:35
C++实现简单酒店管理系统
2021-07-31 13:52:40
![](https://img.aspxhome.com/file/2023/9/122579_0s.jpg)
Java实现在PPT中创建SmartArt图形的示例代码
2022-01-16 06:22:52
![](https://img.aspxhome.com/file/2023/3/62343_0s.jpg)
IDEA教程创建SpringBoot前后端分离项目示例图解
2022-06-30 02:41:10
![](https://img.aspxhome.com/file/2023/4/63424_0s.png)
Android Walker登录记住密码页面功能实现
2023-12-16 05:45:03
![](https://img.aspxhome.com/file/2023/6/137436_0s.jpg)
Java注解详细介绍
2021-10-09 19:52:28
Netty分布式高性能工具类异线程下回收对象解析
2023-07-09 21:02:43
![](https://img.aspxhome.com/file/2023/2/99882_0s.png)
C++智能指针实例详解
2021-12-18 00:38:59
SpringMVC使用RESTful接口案例详解
2022-08-08 23:41:52
Android Service启动过程完整分析
2021-11-25 11:02:44
![](https://img.aspxhome.com/file/2023/9/137989_0s.jpg)
Unity实现攻击范围检测并绘制检测区域
2023-09-18 21:59:10
![](https://img.aspxhome.com/file/2023/7/67557_0s.jpg)
浅谈android nexus私服的使用
2022-09-24 01:30:59
![](https://img.aspxhome.com/file/2023/0/139050_0s.png)
Java中Map与JSON数据之间的互相转化
2021-10-26 01:56:19
![](https://img.aspxhome.com/file/2023/5/104065_0s.jpg)
java的NIO管道用法代码分享
2022-05-01 23:52:53
![](https://img.aspxhome.com/file/2023/7/105917_0s.jpg)
C#中 MessageBox的使用技巧
2023-06-25 16:15:48
![](https://img.aspxhome.com/file/2023/2/85682_0s.webp)
C#9.0推出的4个新特性介绍
2021-10-10 07:49:29
Spring中AOP概念与两种动态代理模式原理详解
2023-04-13 01:21:54
![](https://img.aspxhome.com/file/2023/2/119152_0s.png)
Java中常见的编码集问题总结
2023-11-29 01:40:04
![](https://img.aspxhome.com/file/2023/5/60115_0s.png)