高级UI

【项目FJU】自定义Toast

2019-08-06  本文已影响0人  秀叶寒冬

【项目FJU】自定义Toast

系统自带的Toast因为没有界面,往往无法满足我们自己需求,所以经常需要自己定制Toast来实现我们想要的效果。

1 效果图

截图如下:


1564844266921.png
1564844725068.png

2 源码

package com.yds.jianshu.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.yds.jianshu.utils.widget.constance.CommonImageType;

/**
 * Created by yds
 * on 2019/8/3.
 */
public class FToast {
    /**
     * @param context
     * @param text 提示内容
     * @return
     */
    public static Toast makeText(Context context, String text) {
        return makeText(context, text, Toast.LENGTH_SHORT);
    }

    /**
     *
     * @param context
     * @param text 提示内容
     * @param type 图片显示类型{@link CommonImageType}
     * @return
     */
    public static Toast makeText(Context context, String text,CommonImageType type) {
        return makeText(context, text, Toast.LENGTH_SHORT,type);
    }

    /**
     *
     * @param context
     * @param text 提示内容
     * @param duration 显示时长,对应 Toast.LEHGTH_SHORT和Toast.LENGTH_LONG
     * @return
     */
    public static Toast makeText(Context context, String text, int duration) {
        return makeText(context, text, duration, CommonImageType.DEFAULT);
    }


    /**
     *
     * @param context
     * @param text 提示内容
     * @param duration 显示时长,对应 Toast.LEHGTH_SHORT和Toast.LENGTH_LONG
     * @param type 图片显示类型{@link CommonImageType}
     * @return
     */
    public static Toast makeText(Context context, String text, int duration, CommonImageType type) {
        int imageViewResID = CommonImageType.getCommonImageResID(type);

        return makeText(context, text, duration, imageViewResID);
    }

    /**
     *
     * @param context
     * @param text 提示内容
     * @param duration 显示时长,对应 Toast.LEHGTH_SHORT和Toast.LENGTH_LONG
     * @param imageViewResID 图片资源ID
     * @return
     */
    public static Toast makeText(Context context, String text, int duration, int imageViewResID) {
        ObjectUtil.requireNonNull(context, "context is null");
        if (text == null) {
            text = "提示信息为空";
        }
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView = inflater.inflate(R.layout.f_toast, null);
        TextView mTextView = mView.findViewById(R.id.toast_msg);
        ImageView mImageView = mView.findViewById(R.id.toast_iv);
        if (imageViewResID > 0){
            mImageView.setImageResource(imageViewResID);
        }
        mTextView.setText(text);
        Toast mToast = new Toast(context);
        mToast.setGravity(Gravity.CENTER,0,0);
        mToast.setDuration(duration);
        mToast.setView(mView);

        return mToast;

    }


}
package com.yds.jianshu.utils.widget.constance;

import com.example.myapplication.R;

/**
 * Created by yds
 * on 2019/8/3.
 */
public enum CommonImageType {
    SUCCESS,//成功
    FAIL,//失败
    WARN,//警告
    ERROR,//错误
    DEFAULT,//默认
    YES,//是
    NO;//否

    public static int getCommonImageResID(CommonImageType type) {
        switch (type) {
            case FAIL:
            case WARN:
                return R.drawable.f_toast_alert;
            case SUCCESS:
            case YES:
                return R.drawable.f_toast_yes;
            case NO:
            case ERROR:
                return R.drawable.f_toast_no;
            default:
                return R.drawable.f_toast_alert;

        }
    }

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:minWidth="120dp"
    android:minHeight="120dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/f_toast_bg"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingTop="16dp"
    android:paddingBottom="8dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">

    <ImageView
        android:id="@+id/toast_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/f_toast_alert" />

    <TextView
        android:id="@+id/toast_msg"
        android:layout_marginTop="16dp"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/f_font_light_gray" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#c0000000" />
    <corners android:topLeftRadius="5dp"
        android:topRightRadius="5dp"
        android:bottomRightRadius="5dp"
        android:bottomLeftRadius="5dp"/>
</shape>
package com.yds.jianshu.utils;

/**
 * Created by yds
 * on 2019/8/2.
 */
public class ObjectUtil {
    public static<T> T requireNonNull(T object,String message){
        if(object == null){
            throw new NullPointerException(message);
        }
        return object;
    }
}

FToast.makeText(MainActivity.this,"调用测试", CommonImageType.ERROR).show();

3 源码地址

https://github.com/Yedongsheng/Jianshu/tree/develop

上一篇 下一篇

猜你喜欢

热点阅读