官方封装的Toast
Toast.makeText(this, "提示", Toast.LENGTH_SHORT).show;
自定义Toast
一、思路:使用Toast对象的setView()方法
二、实现
1、首先准备一个Toast的布局文件toast_msg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout"
android:orientation="vertical"
android:background="@drawable/toast_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/toast_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:paddingLeft="10dp"
android:layout_marginRight="10dp"
android:text=""
android:textColor="#ffffff"/>
</LinearLayout>
Toast的背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 背景渐变-->
<gradient
android:angle="-90"
android:endColor="#330000ff"
android:startColor="#3300ff00"/>
<corners android:radius="5dp"/>
<!-- 边框-->
<stroke
android:width="1dp"
android:color="#eeeeee"/>
</shape>
2、封装一个CustomToast类对象,通过单例模式获取。
private static CustomToast instance;
public static CustomToast getInstance(){
if (instance == null){
synchronized (CustomToast.class){
if (instance == null){
instance = new CustomToast();
}
}
}
return instance;
}
3、编写一个cancle()方法,每次显示新的内容时,就取消显示上次的
private Toast toast;
public void cancle(){
if (toast != null){
toast.cancel();
toast = null;
}
}
4、自定义布局
private final int MARGIN_DP = 200;
/**
* 显示自定义布局
* @param context 上下文对象
* @param msg 显示内容
* @param gravity 显示位置
*/
public void showToastCustom(Context context,String msg,int gravity){
cancle();
if (TextUtils.isEmpty(msg)){
return;
}
View layout = View.inflate(context,R.layout.toast_msg,null);//获取布局文件
TextView txtMsg = layout.findViewById(R.id.toast_txt); //获取需要显示的文字
txtMsg.setText(msg);//设置文字
toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT); //显示时长
//判断需要显示的位置
//setGravity(参数1:显示的位置, 参数2:X方向偏移, 参数3:Y方向偏移);
if (gravity == Gravity.TOP){
toast.setGravity(gravity,0,MARGIN_DP);
}else if (gravity == Gravity.BOTTOM){
toast.setGravity(gravity,0,MARGIN_DP);
}else {
toast.setGravity(gravity,0,0);
}
toast.setView(layout);
toast.show(); //记得要把Toast显示出来
}
5、使用
CustomToast.getInstance().showToastCustom(this,"显示顶部", Gravity.TOP);
CustomToast.getInstance().showToastCustom(this,"显示底部", Gravity.BOTTOM);
CustomToast.getInstance().showToastCustom(this,"显示中间", Gravity.CENTER);
7、效果
8、CustomToast完成代码
public class CustomToast {
private static CustomToast instance;
private Toast toast;
private final int MARGIN_DP = 200;
public CustomToast() {
}
public static CustomToast getInstance(){
if (instance == null){
synchronized (CustomToast.class){
if (instance == null){
instance = new CustomToast();
}
}
}
return instance;
}
public void cancle(){
if (toast != null){
toast.cancel();
toast = null;
}
}
/**
* 显示自定义布局
* @param context 上下文对象
* @param msg 显示内容
* @param gravity 显示位置
*/
public void showToastCustom(Context context,String msg,int gravity){
cancle();
if (TextUtils.isEmpty(msg)){
return;
}
View layout = View.inflate(context,R.layout.toast_msg,null);//获取布局文件
TextView txtMsg = layout.findViewById(R.id.toast_txt); //获取需要显示的文字
txtMsg.setText(msg);//设置文字
toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT); //显示时长
//判断需要显示的位置
//setGravity(参数1:显示的位置, 参数2:X方向偏移, 参数3:Y方向偏移);
if (gravity == Gravity.TOP){
toast.setGravity(gravity,0,MARGIN_DP);
}else if (gravity == Gravity.BOTTOM){
toast.setGravity(gravity,0,MARGIN_DP);
}else {
toast.setGravity(gravity,0,0);
}
toast.setView(layout);
toast.show(); //记得要把Toast显示出来
}
}
撒花~~