Android 彩色Toast的实现代码

 更新时间:2018年10月28日 09:18:54   作者:浮云Cloud  
这篇文章主要介绍了Android 彩色Toast的实现代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

Android默认的Toast太丑了,我们来封装一个花里胡哨的Toast吧,就叫ColoredToast。

Github:https://github.com/imcloudfloating/DesignApp

效果:

Toast有一个setView方法,通过它我们可以设置自定义的布局,这里我只是加入了改变背景色,如果你有其它需求,比如加上图标也是可以的。

布局文件:一个FrameLayout和显示消息的TextView

<?xml version="." encoding="utf-"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   <TextView
     android:id="@+id/toast_message"
     android:layout_width="wrap_content"
     android:layout_height="dp"
     android:paddingStart="dp"
     android:paddingEnd="dp"
     android:gravity="center"
     android:textSize="sp"
     tools:text="This is a toast message" />
 </FrameLayout>

2.Java代码:

用LayoutInflater来加载布局,然后用setView将布局设置为Toast的根View,通过自定义方法来设置Toast的消息和背景色,这里背景色是给TextView设置的,假如你想加上图标和其它元素,通过findViewById来设置即可。

这里我用的是GradientDrawable来作为Toast的背景,setColor方法背景色,setCornerRadius设置圆角半径,最后将他作为TextView的背景就可以了。如果你不想用它,也可以直接使用xml文件来作为背景,不过这样就不方便灵活设置颜色了。

 package com.cloud.customviews;
 import android.content.Context;
 import android.graphics.drawable.GradientDrawable;
 import android.support.annotation.ColorRes;
 import android.support.annotation.IntDef;
 import android.support.annotation.NonNull;
 import android.support.annotation.StringRes;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.TextView;
 import android.widget.Toast;
 public class ColoredToast extends Toast {
   @IntDef(value = {
       LENGTH_SHORT,
       LENGTH_LONG
   })
   @interface Duration {}
   private ColoredToast(Context context) {
     super(context);
   }
   public static class Maker {
     private Context mContext;
     private ColoredToast mToast;
     private View mToastView;
     private TextView mTextMessage;
     public Maker(Context context) {
       mContext = context;
       mToast = new ColoredToast(context);
       mToastView = LayoutInflater.from(context).inflate(R.layout.toast_colored, null);
       mTextMessage = mToastView.findViewById(R.id.toast_message);
     }
     /**
     * Set text color and background color for toast by resource id
     */
     public Maker setColor(@ColorRes int textColor, @ColorRes int backgroundColor) {
       GradientDrawable drawable = new GradientDrawable();
       drawable.setColor(mContext.getColor(backgroundColor));
       drawable.setCornerRadius(mTextMessage.getLayoutParams().height / );
       mToastView.setBackground(drawable);
       mTextMessage.setTextColor(mContext.getColor(textColor));
       return this;
     }
     /**
     * Set position
     * @see android.view.Gravity
     */
     public Maker setGravity(int gravity, int xOffset, int yOffset) {
       mToast.setGravity(gravity, xOffset, yOffset);
       return this;
     }
     public ColoredToast makeToast(@StringRes int resId, @Duration int duration) {
       mTextMessage.setText(resId);
       mToast.setView(mToastView);
       mToast.setDuration(duration);
       return mToast;
     }
     public ColoredToast makeToast(@NonNull String text, @Duration int duration) {
       mTextMessage.setText(text);
       mToast.setView(mToastView);
       mToast.setDuration(duration);
       return mToast;
     }
   }
 }

相关文章

  • Android 完全退出当前应用程序的四种方法

    Android 完全退出当前应用程序的四种方法

    Android程序有很多Activity,比如说主窗口A,调用了子窗口B,如果在B中直接finish(), 接下里显示的是A。在B中如何关闭整个Android应用程序呢?本人总结了几种比较简单的实现方法
    2016-02-02
  • Android中@id和@+id及@android:id的区别介绍

    Android中@id和@+id及@android:id的区别介绍

    这篇文章主要给大家介绍了关于Android中@id和@+id及@android:id的区别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • android基于ListView和CheckBox实现多选和全选记录的功能

    android基于ListView和CheckBox实现多选和全选记录的功能

    本篇文章主要介绍了android基于ListView和CheckBox实现多选和全选记录的功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-11-11
  • 微信小程序 跳转页面的两种方法详解

    微信小程序 跳转页面的两种方法详解

    这篇文章主要介绍了微信小程序 跳转页面的两种方法详解的相关资料,需要的朋友可以参考下
    2017-01-01
  • Android利用MPAndroidChart绘制曲线图表的基础教程

    Android利用MPAndroidChart绘制曲线图表的基础教程

    最近在项目中要用到曲线图,于是在网上找了很多很多,有AChartengine,MPAndroidChart,helloChart等等,我还用过基于html5的jsChart来做过,不过最终还是选择了MPAndroidChart来做本文介绍了Android利用MPAndroidChart绘制曲线图表的基础教程,需要的朋友可以参考下。
    2018-03-03
  • 详解Flutter中StatefulBuilder组件的使用

    详解Flutter中StatefulBuilder组件的使用

    StatefulBuilder小部件可以在这些区域的状态发生变化时仅重建某些小区域而无需付出太多努力。本文将来详细讲讲它的使用,需要的可以参考一下
    2022-05-05
  • Android仿QQ好友列表实现列表收缩与展开

    Android仿QQ好友列表实现列表收缩与展开

    这篇文章主要介绍了Android仿QQ好友列表实现列表收缩与展开,感兴趣的小伙伴们可以参考一下
    2015-12-12
  • Android读取本地json文件的方法(解决显示乱码问题)

    Android读取本地json文件的方法(解决显示乱码问题)

    这篇文章主要介绍了Android读取本地json文件的方法,结合实例形式对比分析了解决显示乱码问题的方法,需要的朋友可以参考下
    2016-06-06
  • Android中各种Time API详细

    Android中各种Time API详细

    这篇文章要分享的是Android中各种Time API, SystemClock.uptimeMillis()、System.nanoTime(),下面我们就来看看他们到底有什么区别吧
    2021-10-10
  • flutter 微信聊天输入框功能实现

    flutter 微信聊天输入框功能实现

    这篇文章主要介绍了flutter 微信聊天输入框功能实现,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03

最新评论