如何在Android中实现彩色Toast

发布时间:2021-05-24 18:09:00 作者:Leah
来源:亿速云 阅读:163

本篇文章为大家展示了如何在Android中实现彩色Toast,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

上述内容就是如何在Android中实现彩色Toast,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Android 提示信息Toast
  2. 将android中如何调整Toast位置?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android toast

上一篇:怎么在android中利用FragmentTabhost实现导航分页

下一篇:怎么在Android中利用ProgressButton实现进度条按钮

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》