参考文章 原文链接:http://download.csdn.net/detail/u010566681/9612322
文章内容的核心思路是通过Toast类的getView方法获取到Toast的View,通过得到的view.findbyid查找安卓系统为Toast定义的TextView资源(android.R.id.message)。
而后针对TextView类进行操作即可。
更新代码也封装进了原作者的ToastUtil类中,使用时首先通过
ToastUtil toastUtil = new ToastUtil(); //创建toastUtil对象
toastUtil.Short(context,getResources().getString(id)) //调用Short方法传入提示内容
.setToastBackground(Color.WHITE,R.drawable.toast_radius) //调用原文的setToastBackground方法设置颜色及背景
.setToastSize(400,200,24).show(); //调用新增的setToastSize设置Toast的宽,高及字体大小。
对比原来的类增加设置Toast大小及字体大小的类:
'''
/*
* 设置Toast大小
* */
public ToastUtil setToastSize(int width,int height,int textSize){
View view = toast.getView();
if(view != null){
TextView message = view.findViewById(android.R.id.message);
message.setWidth(width);
message.setHeight(height);
message.setGravity(Gravity.CENTER);
if(textSize<20)
message.setTextSize(20);
else
message.setTextSize(textSize);
// message.setAutoSizeTextTypeWithDefaults(AUTO_SIZE_TEXT_TYPE_UNIFORM);
}
return this;
}
'''
完整类代码如下:
'''
package com.telecom.geoquiz;
/**
* Created by apple on 16/8/24.
*/
import android.content.Context;
import android.graphics.Color;
import android.graphics.fonts.FontVariationAxis;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import static android.widget.TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM;
/**
* 作者:ban on 16/8/24 17:55
*/
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 setToastSize(int width,int height,int textSize){
View view = toast.getView();
if(view != null){
TextView message = view.findViewById(android.R.id.message);
message.setWidth(width);
message.setHeight(height);
message.setGravity(Gravity.CENTER);
if(textSize<20)
message.setTextSize(20);
else
message.setTextSize(textSize);
// message.setAutoSizeTextTypeWithDefaults(AUTO_SIZE_TEXT_TYPE_UNIFORM);
}
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;
}
}
'''