由于吐司Toast在APP中用的非常多,所以有必要对他进行封装一下,下面是封装好的代码,可以直接拿去使用
效果图
源代码分析
对Toast封装之前,先分析一下源代码
Toast.makeText()
public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
com.android.internal.R.layout.transient_notification
<?xml version="2.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="?android:attr/toastFrameBackground">
<TextView
android:id="@android:id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:textAppearance="@style/TextAppearance.Toast"
android:textColor="@color/bright_foreground_dark"
android:shadowColor="#BB000000"
android:shadowRadius="2.75"
/>
</LinearLayout>
那么我们可以发现,我们对Toast进行自定义修改其实就是在改变Toast根布局和message这个TextView
Toast的另外一种显示模式就是自定义布局显示。这个方法不调用Toast.makeText()方法,而是new一个Toast对象,然后调用setView()方法。
当然自定义布局就不会加载transient_notification布局了。
封装功能
对以下常用Toast进行了封装:
1.设置Toast字体及背景颜色
2.设置Toast字体及背景
3.短时间显示Toast
4.长时间显示Toast
5.自定义显示Toast时间
6.向Toast中添加自定义view
7.完全自定义布局Toas
封装的代码展示
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class ToastUtil {
private Toast toast;
private LinearLayout toastView;
/**
* 修改原布局的Toast
*/
public ToastUtil() {
}
/**
* 完全自定义布局Toast
*
* @param context
* @param view
*/
public ToastUtil(Context context, View view, int duration) {
toast = new Toast(context);
toast.setView(view);
toast.setDuration(duration);
}
/**
* 向Toast中添加自定义view
*
* @param view
* @param postion
* @return
*/
public ToastUtil addView(View view, int postion) {
toastView = (LinearLayout) toast.getView();
toastView.addView(view, postion);
return this;
}
/**
* 设置Toast字体及背景颜色
*
* @param messageColor
* @param backgroundColor
* @return
*/
public ToastUtil setToastColor(int messageColor, int backgroundColor) {
View view = toast.getView();
//设置Toast背景颜色为透明
view.setBackgroundColor(Color.TRANSPARENT);
if (view != null) {
TextView message = ((TextView) view.findViewById(android.R.id.message));
message.setBackgroundColor(backgroundColor);
message.setTextColor(messageColor);
}
return this;
}
/**
* 设置Toast字体及背景
*
* @param messageColor
* @param background
* @return
*/
public ToastUtil setToastBackground(int messageColor, int background) {
View view = toast.getView();
//设置Toast背景颜色为透明
view.setBackgroundColor(Color.TRANSPARENT);
if (view != null) {
TextView message = ((TextView) view.findViewById(android.R.id.message));
message.setBackgroundResource(background);
message.setTextColor(messageColor);
}
return this;
}
/**
* 短时间显示Toast
*/
public ToastUtil Short(Context context, CharSequence message) {
if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
}
return this;
}
/**
* 短时间显示Toast
*/
public ToastUtil Short(Context context, int message) {
if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
}
return this;
}
/**
* 长时间显示Toast
*/
public ToastUtil Long(Context context, CharSequence message) {
if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
}
return this;
}
/**
* 长时间显示Toast
*
* @param context
* @param message
*/
public ToastUtil Long(Context context, int message) {
if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
}
return this;
}
/**
* 自定义显示Toast时间
*
* @param context
* @param message
* @param duration
*/
public ToastUtil Indefinite(Context context, CharSequence message, int duration) {
if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, duration);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(duration);
}
return this;
}
/**
* 自定义显示Toast时间
*
* @param context
* @param message
* @param duration
*/
public ToastUtil Indefinite(Context context, int message, int duration) {
if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, duration);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(duration);
}
return this;
}
/**
* 显示Toast
*
* @return
*/
public ToastUtil show() {
toast.show();
return this;
}
/**
* 获取Toast
*
* @return
*/
public Toast getToast() {
return toast;
}
}