自定义吐司

一.概述:
由于公司需要做一个如下图的弹窗,需要每个页面都进行提示,原想法在基类的Activity中加上这个布局,使用visibility来实现。发现工作量太大,还是来个吐司比较快。于是自定义一个吐司搞定。
这里写图片描述
二.具体实现:
1.java代码:

public class CustomToast {
private final TextView mTextView;
private final String msg;
private Context context;
private Toast toast;
private TimeCount timeCount;
private boolean canceled=true;
private Handler mHandler = new Handler();
public CustomToast(Context context,String msg) {
    this.context=context;
    this.msg=msg;
    LayoutInflater inflater =      (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       //自定义布局
    View mView = inflater.inflate(R.layout.custom_toast, null);
    mTextView = (TextView)mView.findViewById(R.id.toast_msg);
    mTextView.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(200),dpToPx(40)));
    mTextView.setText(msg);

    if (toast == null) {
        toast = new Toast(context);
    }
    toast.setGravity(Gravity.TOP, 0,dpToPx(10));
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(mView);

}
/**
 * 位置设置
 * @param gravity 位置
 * @param xOffset 距离横向坐标值
 * @param yOffset 距离纵向坐标值
 * */
public void setLocation(int gravity,int xOffset,int yOffset){
    toast.setGravity(gravity,xOffset,yOffset);
}
/**
 * 大小设置
 * @param with 宽度
 * @param height 高度
 * 
 * */
public void setToastSize(int with ,int height){
    mTextView.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(with),dpToPx(height)));
}

/**
 * 文字颜色设置
 * @param color 
 *
 * */
public void setTextColor(int color){
    mTextView.setTextColor(context.getResources().getColor(color));
}
public void setTextColor(String color){
    mTextView.setTextColor(Color.parseColor(color));
}

/**
 * 背景色设置
 * @param color
 *
 * */
public void setBackground(int color){
    mTextView.setBackgroundResource(color);
}
public void setBackground(String color){
    mTextView.setBackgroundColor(Color.parseColor(color));
}
public void setBackground(Drawable drawable){
    mTextView.setBackground(drawable);
}
/**
 * dp转px方法
 * @param dp dp值
 *
 * */
private  int dpToPx( int dp) {
    if (context == null) {
        return -1;
    }
    return (int)(dp * context.getResources().getDisplayMetrics().density);
}

public void show() {
    toast.show();

}
public void show(int duration) {
    timeCount = new TimeCount(duration, 1000);
    if (canceled) {
        timeCount.start();
        canceled = false;
        showUntilCancel();
    }
}
/**
 * 自定义定时器
 * */
private class TimeCount extends CountDownTimer {

    public TimeCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval); //millisInFuture总计时长,countDownInterval时间间隔(一般为1000ms)
    }

    @Override
    public void onTick(long millisUntilFinished) {
        mTextView.setText(msg);
    }

    @Override
    public void onFinish() {
        hide();
    }
}
private void hide() {
    if (toast != null) {
        toast.cancel();
    }
    canceled = true;
}
private void showUntilCancel() {
    if (canceled) {
        return;
    }
    toast.show();
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            showUntilCancel();
        }
    }, Toast.LENGTH_LONG);
}

2.布局文件:
根布局得用LinearLayout否者设置宽度会失败:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
    android:background="#fef0a1"
    android:id="@+id/toast_msg"
    android:layout_width="match_parent"
    android:gravity="center"
    android:textSize="15sp"
    android:textColor="#d0ac05"
    android:layout_height="wrap_content" />
  </LinearLayout>

三.使用:

     CustomToast customToast=new CustomToast(MainActivity.this,"有新的消息呦~");
            customToast.setLocation(Gravity.TOP,0, (int)  
                    (48*getResources().getDisplayMetrics().density));
            customToast.setToastSize(pxToDpInt(MainActivity.this,mScreenWidth),40);
            customToast.setBackground("#F6D41C");
            customToast.setTextColor("#CC7832");
       customToast.show(3000);  

更改颜色

使用shape背景

设置图片背景

搞定ok!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值