android 自定义Toast 实践

android 原生Toast源码并不多,照虎画猫的难度并不大。

刚好最近有一个需求是弹出的 toast需要上面有一个图标,下面是文字。在屏幕上居中显示。

其实代码很少。就两个文件,一个是自定义的布局,一个是.java文件。

custom_toast_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/wrapper_toast_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="#000"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:id="@+id/img_toast_img"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher_round" />

    <TextView
        android:id="@+id/tv_toast_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="8dp"
        android:textColor="#fff"
        android:textSize="16sp"
        tools:text="@string/app_name" />
</LinearLayout>

ToastHelper.java

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
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.abc.demo.R;

public class ToastHelper {

    private ToastHelper() {
    }

    /**
     * 直接使用,不需要使用 builder
     *
     * @param context  context
     * @param text     text
     * @param duration duration
     */
    public static void show(Context context, CharSequence text, int duration) {
        Toast toast = new Toast(context);
        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(R.layout.custom_toast_layout, null);
        TextView tv = v.findViewById(R.id.tv_toast_text);
        tv.setText(text);
        toast.setView(v);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.setDuration(duration);
        toast.show();
    }

    /**
     * 直接使用,不需要使用 builder
     *
     * @param context  context
     * @param text     text
     * @param duration duration
     */
    public static void show(Context context, @StringRes int text, int duration) {
        Toast toast = new Toast(context);
        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(R.layout.custom_toast_layout, null);
        TextView tv = v.findViewById(R.id.tv_toast_text);
        tv.setText(text);
        toast.setView(v);
        toast.setDuration(duration);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.show();
    }

    public static class Builder {

        private final View wrapperLayout;

        private final Toast toast;
        public Builder(Context context) {
            LayoutInflater inflate = (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            wrapperLayout = inflate.inflate(R.layout.custom_toast_layout, null);
            toast = new Toast(context);
            toast.setView(wrapperLayout);
        }

        public Builder setWrapperBackgroundColor(@ColorInt int color) {
            wrapperLayout.setBackgroundColor(color);
            return this;
        }

        public Builder setWrapperBackground(Drawable background) {
            wrapperLayout.setBackground(background);
            return this;
        }

        public Builder setWrapperBackgroundResource(@DrawableRes int resid) {
            wrapperLayout.setBackgroundResource(resid);
            return this;
        }

        public Builder setTextColor(@ColorInt int color) {
            TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
            tv.setTextColor(color);
            return this;
        }

        public Builder setTextSize(float size) {
            TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
            tv.setTextSize(size);
            return this;
        }

        public Builder setTextSize(int unit, float size) {
            TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
            tv.setTextSize(unit, size);
            return this;
        }

        public Builder setText(@StringRes int text) {
            TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
            tv.setText(text);
            return this;
        }

        public Builder setText(CharSequence text) {
            TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
            tv.setText(text);
            return this;
        }

        public Builder setIcon(@DrawableRes int resId) {
            ImageView image = wrapperLayout.findViewById(R.id.img_toast_img);
            image.setImageResource(resId);
            return this;
        }

        @NonNull
        public Context getContext() {
            return this.wrapperLayout.getContext();
        }

        public Toast build() {
            return toast;
        }

        public Toast show(@StringRes int text, int duration) {
            Toast toast = build();
            TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
            tv.setText(text);
//            toast.setView(wrapperLayout);
            toast.setDuration(duration);
            toast.setGravity(Gravity.CENTER,0,0);
            toast.show();
            return toast;
        }

        public Toast show(CharSequence text, int duration) {
            Toast toast = build();
            TextView tv = wrapperLayout.findViewById(R.id.tv_toast_text);
            tv.setText(text);
//            toast.setView(wrapperLayout);
            toast.setDuration(duration);
            toast.setGravity(Gravity.CENTER,0,0);
            toast.show();
            return toast;
        }
    }
}

其实这里的Builder有点鸡肋。一般来讲,这些属性完全应该在xml里面写死。不过,说不定对于icon 可能要动态设置一下,如果仅仅是这样的话,可以只添加一个设置 icon 的方法,也可以不使用 Builder.

此处的 Builder也是对AlertDialog.Builder的一次照虎画猫

源码其实就是上面这些了,就不放链接了。

end: 发现对于这些类似于 feature的东西,tagbranch好用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值