自定义Toast

作者: 唠嗑008 | 来源:发表于2017-07-12 14:55 被阅读148次

    github地址(欢迎下载完整Demo)

    https://github.com/zhouxu88/CustomToast

    今天为大家带来的是建造者模式来实现自定义Toast

    如果还不清楚什么是建造者模式,可以看看我的另外一篇文章
    设计模式---建造者模式

    效果图:

    自定义toast.jpg

    1.Toast源码分析

    Toast的显示只需要一句代码

    Toast.makeText(this,"下载失败",Toast.LENGTH_SHORT).show();
    

    下面就对这句代码分析,源码是这样的:

     public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
            Toast result = new Toast(context);
            LayoutInflater inflate = (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
            TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
            tv.setText(text);
            result.mNextView = v;
            result.mDuration = duration;
            return result;
        }
    

    transient_notification这个布局文件代码是这样的

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?android:attr/toastFrameBackground">
    
    <TextView
    android:id="@android:id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_gravity="center_horizontal"
    android:textAppearance="@style/TextAppearance.Toast"
    android:textColor="@color/bright_foreground_dark"
    android:shadowColor="#BB000000"
    android:shadowRadius="2.75"
    />
    </LinearLayout>
    

    看了一下,尼玛,很简单嘛,这个方法无非就是创建了一个Toast对象,然后获取一个布局填充器,然后将系统布局文件转换为View的对象,再使用这个View对象里边的TextView控件显示设置的文本,最后返回的也是这个Toast对象。

    既然是这样,那么我们只要根据这个布局,比如修改Toast根布局的背景、TextView、或者直接重新写个布局,然后在通过布局填充器获取这些控件,最后调用相应的方法设置一下,是不是就可以搞事情了。

    2、CustomToast

    package com.zx.customtoast;
    
    import android.content.Context;
    import android.graphics.Color;
    import android.graphics.drawable.GradientDrawable;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    
    /**
     * 自定义Toast(建造者模式)
     * <p>
     * 作者: 周旭 on 2017/7/10/0010.
     * 邮箱:374952705@qq.com
     * 博客:http://www.jianshu.com/u/56db5d78044d
     */
    
    public class CustomToast {
    
        private static Toast mToast;
        private static Context mContext;
        
    
        public CustomToast(Builder builder) {
            if (mToast == null) {
                mToast = new Toast(mContext);
            }
            View view = LayoutInflater.from(mContext).inflate(R.layout.layout_custom_toast, null);
            TextView textView = (TextView) view.findViewById(R.id.title_tv);
            LinearLayout rootLayout = (LinearLayout) view.findViewById(R.id.root_layout);
            textView.setText(builder.text);
            mToast.setView(view);
            mToast.setDuration(Toast.LENGTH_SHORT);
            if (builder.textColor != 0) {
                textView.setTextColor(builder.textColor);
            } else {
                //默认白色
                textView.setTextColor(Color.WHITE);
            }
            // 获取背景颜色,并且改变颜色  
            GradientDrawable drawable = (GradientDrawable) rootLayout.getBackground();
            if (builder.backgroundColor != 0) {
                drawable.setColor(builder.backgroundColor);
            } else {
                //默认的背景颜色
                drawable.setColor(mContext.getResources().getColor(R.color.color_3));
            }
            if (builder.gravity != 0) {
                mToast.setGravity(builder.gravity, 0, 0);
            } else {
                //默认,居中,显示
                mToast.setGravity(Gravity.CENTER, 0, 0);
            }
        }
        
    
        /**
         * 显示Toast
         */
        public void show() {
            mToast.show();
        }
    
        /**
         * builder
         */
        public static class Builder {
            private String text; //提示文言
            private int textColor; //文字颜色
            private int gravity; //相对窗体的位置
            private int backgroundColor; //toast背景颜色
    
    
            public Builder(Context context) {
                mContext = context;
            }
    
            public Builder setText(String text) {
                this.text = text;
                return this;
            }
    
            public Builder setGravity(int gravity) {
                this.gravity = gravity;
                return this;
            }
    
            public Builder setTextColor(int textColor) {
                this.textColor = textColor;
                return this;
            }
    
            public Builder setBackgroundColor(int backgroundColor) {
                this.backgroundColor = backgroundColor;
                return this;
            }
    
            public CustomToast build() {
                return new CustomToast(this);
            }
        }
    }
    

    这里,我是通过建造者模式来设置Toast的文言,文字颜色,背景,相对位置等属性,这样比较灵活,你可以自定义,也可以使用默认的颜色,背景

    3、最后显示Toast的调用如下

    //默认设置
     new CustomToast.Builder(mContext).setText("加入购物车成功").build().show();
    
    或者
    //自定义的
     new CustomToast.Builder(mContext)
                            .setText("加入购物车成功")
                            .setBackgroundColor(Color.RED)
                            .build()
                            .show();
    
    

    方形的Toast看上去不太喜欢,我自定义了一个名为toast_radius.xml的背景,代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <!-- 填充的颜色 -->
        <solid android:color="#333333" />
    
        <!-- android:radius 弧形的半径 -->
        <corners android:radius="10dp" />
    
    </shape>
    

    相关文章

      网友评论

        本文标题:自定义Toast

        本文链接:https://www.haomeiwen.com/subject/kijbhxtx.html