(三)——自定义不一样的Toast
最后更新于:2022-04-01 09:36:12
# Android特效专辑(三)——自定义不一样的Toast
~~~
大家都知道,Android的控件有时候很难满足我们的需求,所以我们需要自定义View。自定义的方式很多,有继承原生控件也有直接自定义View的,今天写的是自定义的Toast,当然,这个不是复写Toast,是换一种表达形式,哈哈,后续会增加自定义View的方方面面
~~~
> 这里啰嗦几句,我看到网上很多教程写的很不详细,就像是直接CV过来的一样,所以有时间我就会把我需要的东西写起来,然后尽可能的描述的清楚一点,以后我工作用到了就可以直接拿来用了,同时也可以把有用的星系分享给大家,这就是我写博客 的初衷了,经常去墙外面找的好例子,我也是摸透了才放在我博客上,如果以后有疑问或者不懂的,可以加我QQ沟通
~~~
先来看看效果图吧
~~~
![这里写图片描述](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-24_56cd2c1718703.jpg "")
~~~
是不是挺好看的,如果你看过Toast的源码就会知道,他主要是用了WindowManager,我们也直接来写一个
~~~
### TabToast
~~~
package com.lgl.toast;
import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class TabToast {
/**
* Toast字体大小
*/
private static final float DEFAULT_TEXT_SIZE = 14;
/**
* Toast字体颜色
*/
private static final int DEFAULT_TEXT_COLOR = 0xffffffff;
/**
* Toast背景颜色
*/
private static final int DEFAULT_BG_COLOR = 0xE6f5695a;
/**
* Toast的高度(单位dp)
*/
private static final float DEFAULT_TOAST_HEIGHT = 50.0f;
private static Context mContext;
private volatile static TabToast mInstance;
private static Toast mToast;
private View layout;
private TextView tv;
public TabToast(Context context) {
mContext = context;
}
/**
* 单例模式
*
* @param context
* 传入的上下文
* @return TabToast实例
*/
private static TabToast getInstance(Context context) {
if (mInstance == null) {
synchronized (TabToast.class) {
if (mInstance == null) {
mInstance = new TabToast(context.getApplicationContext());
}
}
}
return mInstance;
}
private static void getToast(int duration) {
//设置位置int gravity, int xOffset, int yOffset
if (mToast == null) {
mToast = new Toast(mContext);
mToast.setGravity(Gravity.BOTTOM, 0, 0);
//设置显示时间
mToast.setDuration(duration == Toast.LENGTH_LONG ? Toast.LENGTH_LONG
: Toast.LENGTH_SHORT);
}
}
public static void makeText(Context context, String text) {
makeText(context, text, Toast.LENGTH_SHORT);
}
public static void makeText(Context context, String text, int duration) {
getInstance(context);
getToast(duration);
if (mInstance.layout == null || mInstance.tv == null) {
LinearLayout container = new LinearLayout(mContext);
LinearLayout.LayoutParams rootParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
container.setLayoutParams(rootParams);
container.setBackgroundColor(DEFAULT_BG_COLOR);
container.setGravity(Gravity.CENTER);
mInstance.tv = new TextView(mContext);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(
getScreenWidth(mContext), dp2px(DEFAULT_TOAST_HEIGHT));
mInstance.tv.setLayoutParams(tvParams);
mInstance.tv.setPadding(dp2px(5), dp2px(2), dp2px(5), dp2px(2));
mInstance.tv.setGravity(Gravity.CENTER);
mInstance.tv.setTextColor(DEFAULT_TEXT_COLOR);
mInstance.tv.setMaxLines(2);
mInstance.tv.setEllipsize(TextUtils.TruncateAt.END);
mInstance.tv.setBackgroundColor(DEFAULT_BG_COLOR);
mInstance.tv.setTextSize(DEFAULT_TEXT_SIZE);
container.addView(mInstance.tv);
mInstance.layout = container;
mToast.setView(mInstance.layout);
}
mInstance.tv.setText(text);
mToast.show();
}
/**
* dp转px
*
* @param value
* dp
* @return px
*/
public static int dp2px(float value) {
float scale = mContext.getResources().getDisplayMetrics().density;
return (int) (value * scale + 0.5f);
}
/**
* 获得屏幕宽度
*
* @param context
* Context
* @return px
*/
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
float density = outMetrics.density;
return (int) (outMetrics.widthPixels * density);
}
}
~~~
~~~
然后直接调用就可以了
~~~
~~~
TabToast.makeText(MainActivity.this, "自定义Toast");
~~~
### Demo下载地址:[http://download.csdn.net/detail/qq_26787115/9391760](http://download.csdn.net/detail/qq_26787115/9391760)