您的位置:首页 > 移动开发 > Android开发

Android自定义Toast取代系统的Toast

2015-12-05 10:18 387 查看
为什么要自定义Toast来取代系统的,不知道大家有没有发现这个现象,代码中实现了Toast也show了,为什么有的手机没有显示出来。其实这是因为在部分手机的设置里面有个通知管理,如果你将应用的通知管理禁止掉,你以为只是不显示通知,其实Toast也显示不出来了。在华为荣耀6Plus上,亲测是这个效果。有的时候需要提示用户的行为,结果显示不出来,体验很不好。所以我们要自定义Toast。

直接上代码:

/**

* 自定义Toast,可以自有控制要显示的时间

*

*/

public final class CustomToast {

/**

* 预定义两个持续时间,与系统的Toast保持一致

*/

public static final long LENGTH_SHORT = 1000;

public static final long LENGTH_LONG = 3500;

private long duration = LENGTH_SHORT;

private WindowManager windowManager;

private LinearLayout rootView;

private OnDismissListener onDismissListener;

public CustomToast(Context context) {

// 此处必须是ApplicationContext,因为Activity退出也可以显示

context = context.getApplicationContext();

windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

rootView = new LinearLayout(context);

// 背景设置为圆角矩形

float r = 10;

float[] outerR = new float[] { r, r, r, r, r, r, r, r };

RoundRectShape rr = new RoundRectShape(outerR, null, null);

ShapeDrawable drawable = new ShapeDrawable(rr);

drawable.getPaint().setColor(Color.BLACK);

rootView.setBackgroundDrawable(drawable);

}

/**

*

* @param context

* 上下文

* @param text

* 要显示的文本

* @param duration

* 显示时长,单位:毫秒

*/

public static CustomToast makeText(Context context, CharSequence text, long duration) {

CustomToast toast = new CustomToast(context);

toast.setDuration(duration);

TextView textView = new TextView(context);

int width = UIUtil.dip2px(context, 12);

int height = UIUtil.dip2px(context, 8);

textView.setPadding(width, height, width, height);

textView.setText(text);

textView.setTextColor(Color.WHITE);

textView.setTextSize(16);

toast.setView(textView);

return toast;

}

public static CustomToast makeText(Context context, int stringId, long duration)

throws Resources.NotFoundException {

return makeText(context, context.getResources().getText(stringId), duration);

}

/**

* 显示时长,单位:毫秒

*/

public void setDuration(long duration) {

if (duration > 500) {

this.duration = duration;

}

}

/**

* 可以放入任何视图,不仅仅是文本

*/

public void setView(View view) {

if (view != null && view.getParent() == null) {

this.rootView.addView(view);

}

}

public void setOnDismissListener(OnDismissListener onDismissListener) {

this.onDismissListener = onDismissListener;

}

/**

*

* @param gravity 相对屏幕的参考点

* @see Gravity#TOP

* @see Gravity#BOTTOM

* @see Gravity#LEFT

* @see Gravity#RIGHT

*

* @param xOffset 水平方向上的偏移量

* @param yOffset 竖直方向上的偏移量

*/

public void show(int gravity, int xOffset, int yOffset) {

LayoutParams lp = getDefaultLayoutParams();

lp.gravity = gravity;

lp.x = xOffset;

lp.y = yOffset;

windowManager.addView(rootView, lp);

handleDelayDismiss();

}

public void show() {

show(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM, 0, 100);

}

private WindowManager.LayoutParams getDefaultLayoutParams() {

LayoutParams lp = new WindowManager.LayoutParams();

lp.gravity = Gravity.CENTER;

lp.dimAmount = 0f;

lp.width = WindowManager.LayoutParams.WRAP_CONTENT;

lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

lp.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND

| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE

| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE

| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;

lp.type = WindowManager.LayoutParams.TYPE_TOAST;

// 此值必须设置为透明,否则圆角处会有黑色

lp.format = PixelFormat.TRANSLUCENT;

// 显示动画,此处必须是系统的样式资源

lp.windowAnimations = android.R.style.Animation_Toast;

return lp;

}

private void handleDelayDismiss() {

new Handler().postDelayed(new Runnable() {

public void run() {

dismiss();

}

}, duration);

}

private void dismiss() {

if (rootView != null) {

if (rootView.getParent() != null) {

windowManager.removeView(rootView);

if (onDismissListener != null) {

onDismissListener.onDismiss();

}

}

rootView = null;

}

}

public interface OnDismissListener {

void onDismiss();

}

}

代码并不是很复杂,用法跟系统的Toast方法一样,还可以自定义要显示的样式,并添加了Toast dismiss时的监听。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: