為了滿足産品的各種需求以及美觀度,有時候系統的東西不能滿足時,就需要自定義了,
今天要說的是自定義Toast,下面我就直接貼代碼吧,如果喜歡記得給妹子點個贊哦O(∩_∩)O~~
如有不對的地方,望各路小哥哥,小姐姐,指導
效果圖:(因為我每次看别人的部落格,首先是想要看效果的,所有我寫部落格也是盡量先上圖)我這裡是設定的居中顯示

首先是需要用到的布局,這裡我隻寫我自己用到的布局:
1.toast_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/toast_bg"
android:gravity="center"
android:minWidth="100dp"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="10dp" >
<ImageView
android:id="@+id/iv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:visibility="gone"/>
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/color_white"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
背景圖:toast_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#c8333333" />
<corners
android:bottomLeftRadius="6dp"
android:bottomRightRadius="6dp"
android:topLeftRadius="6dp"
android:topRightRadius="6dp" />
</shape>
2.ToastView
package com.mobivans.wealth.customerview;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.mobivans.wealth.R;
import java.util.Timer;
import java.util.TimerTask;
/**
* @author: 潇潇
* @create on: 2019/4/8
* @describe:自定義ToastView
*/
public class ToastView {
public static Toast toast;
private int time;
private Timer timer;
public ToastView(Context context, String text) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.toast_view, null);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
if (toast != null) {
toast.cancel();
}
toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
}
public ToastView(Context context, int text, boolean hasImg, int resId) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.toast_view, null);
TextView t = (TextView) view.findViewById(R.id.toast_text);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_title);
imageView.setImageResource(resId);
imageView.setVisibility(View.VISIBLE);
t.setGravity(Gravity.CENTER);
t.setText(text);
if (toast != null) {
toast.cancel();
}
toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
}
public ToastView(Context context, String text, boolean hasImg, int resId) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.toast_view, null);
TextView t = (TextView) view.findViewById(R.id.toast_text);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_title);
imageView.setImageResource(resId);
imageView.setVisibility(View.VISIBLE);
t.setGravity(Gravity.CENTER);
t.setText(text);
if (toast != null) {
toast.cancel();
}
toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
}
// 設定toast顯示位置
public void setGravity(int gravity, int xOffset, int yOffset) {
toast.setGravity(Gravity.CENTER, 0, 0); // 居中顯示
// toast.setGravity(gravity, xOffset, yOffset);
}
// 設定toast顯示時間
public void setDuration(int duration) {
toast.setDuration(duration);
}
// 設定toast顯示時間(自定義時間)
public void setLongTime(int duration) {
// toast.setDuration(duration);
time = duration;
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (time - 1000 >= 0) {
show();
time = time - 1000;
} else {
timer.cancel();
}
}
}, 0, 1000);
}
public void show() {
toast.show();
}
public static void cancel() {
if (toast != null) {
toast.cancel();
}
}
}
3.ToastUtils
package com.mobivans.wealth.utils;
import android.content.Context;
import android.view.Gravity;
import com.mobivans.wealth.customerview.ToastView;
/**
* @author: 潇潇
* @create on: 2019/4/8
* @describe:DOTO
*/
public class ToastUtil {
public static ToastView showCenter(Context context, String message) {
ToastView toastView = new ToastView(context, message);
toastView.setGravity(Gravity.CENTER, 0, 0);
toastView.show();
return toastView;
}
public static ToastView showCenter_img(Context context, String message, boolean hasImg, int resId) {
ToastView toastView = new ToastView(context, message, hasImg, resId);
toastView.setGravity(Gravity.CENTER, 0, 0);
toastView.show();
return toastView;
}
}
4.在activity的調用
ToastUtil.showCenter(ToastActivity.this, "toast1");
ToastUtil.showCenter_img(ToastActivity.this, "toast2", true, R.drawable.wujiaoxing);