在开发中toast会经常用到,但是每个品牌的手机toast的展示效果是不同的,为了达到统一的效果,并且根据我们的喜好来制定toast,就需要自定义Toast.

先看使用

IToast.show("这是一个土司哦");

IToast.png

关键代码

Toast toast = new Toast(context);

//设置Toast要显示的位置,水平居中并在底部,X轴偏移0个单位,Y轴偏移70个单位,

toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 70);

//设置显示时间

toast.setDuration(show_length);

toast.setView(view);

toast.show();

xml布局

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="10dp"

android:background="@drawable/toast_shape">

android:id="@+id/toast_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:layout_marginBottom="10dp"

android:layout_marginLeft="20dp"

android:layout_marginRight="20dp"

android:layout_marginTop="10dp"

android:gravity="center"

android:text="一段很长的测试文字"

android:textColor="#fff"

android:textSize="18sp"/>

android:id="@+id/toast_iv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@mipmap/cofe"/>

自定义Toast,并简单封装

package com.example.chenzhen.isimpledemo.helper;

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.chenzhen.isimpledemo.IApplication;

import com.example.chenzhen.isimpledemo.R;

/**

* =================中康================

*

* @Author: 陈振

* @Email : 18620156376@163.com

* @Time : 2016/8/17 11:24

* @Action :自定义的toast工具类

* 1-自定义样式

* 2-内部自动获取上下文

*

* =================中康================

*/

public class IToast {

/**

* 展示toast==LENGTH_SHORT

*

* @param msg

*/

public static void show(String msg) {

show(msg, Toast.LENGTH_SHORT);

}

/**

* 展示toast==LENGTH_LONG

*

* @param msg

*/

public static void showLong(String msg) {

show(msg, Toast.LENGTH_LONG);

}

private static void show(String massage, int show_length) {

Context context = IApplication.getGlobalContext();

//使用布局加载器,将编写的toast_layout布局加载进来

View view = LayoutInflater.from(context).inflate(R.layout.toast_layout, null);

//获取ImageView

ImageView image = (ImageView) view.findViewById(R.id.toast_iv);

//设置图片

image.setImageResource(R.mipmap.cofe);

//获取TextView

TextView title = (TextView) view.findViewById(R.id.toast_tv);

//设置显示的内容

title.setText(massage);

Toast toast = new Toast(context);

//设置Toast要显示的位置,水平居中并在底部,X轴偏移0个单位,Y轴偏移70个单位,

toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 70);

//设置显示时间

toast.setDuration(show_length);

toast.setView(view);

toast.show();

}

}

android中自定义 toast,Android自定义Toast相关推荐

  1. Android中导航栏之自定义导航布局

    Toolbar系列文章导航 Android中导航栏之Toolbar的使用 Android中导航栏之溢出菜单OverflowMenu Android中导航栏之搜索框SearchView Android中 ...

  2. Android中如何自己定义吐司(Toast)

    Android系统里面有个东西很好用,也很常用,那就是Toast,但是长期使用也会发现,Toast有他的不足之处:形式单一,只有文字,风格不变等等,那么要如何自定义一个Toast呢,我们可以先从分析A ...

  3. Android中SimpleAdapter的使用—自定义列表

    本人初学Android,今天研究到Adapter这块感觉挺有意思的,写了个自定义列表进行测试 首先我们新建一个layout列表布局文件,具体布局可以自己设定. 下面贴上我的自定义布局文件代码 1 &l ...

  4. Android中实现Bitmap在自定义View中的放大与拖动

    一基本实现思路: 基于View类实现自定义View –MyImageView类.在使用View的Activity类中完成OnTouchListener接口,实现对MotionEvent事件的监听与处理 ...

  5. android 如何去掉自定义标签页,Android中为TextView增加自定义的HTML标签

    Android中的TextView,本身就支持部分的Html格式标签.这其中包括常用的字体大小颜色设置,文本链接等.使用起来也比较方便,只需要使用Html类转换一下即可.比如: textView.se ...

  6. Android中为TextView增加自定义的HTML标签

    为什么80%的码农都做不了架构师?>>>    Android中的TextView,本身就支持部分的Html格式标签.这其中包括常用的字体大小颜色设置,文本链接等.使用起来也比较方便 ...

  7. android 中dialog对话框,Android中的对话框dialog

    普通对话框 单选对话框 多选对话框 进度条对话框 底部弹出框 1.普通对话框 this 代表当前类 最终继承Context 相当于是子类 getApplicationContext:直接返回的是Con ...

  8. android中oncreate方法,android开发之onCreate( )方法详解

    这里我们只关注一句话:This is where you should do all of your normal static set up.其中我们只关注normal static, normal ...

  9. Android中list常用方法,Android中的常用控件及其基本用法

    TextView的使用方法 布局文件中的配置: 在Activity类中的使用: TextView textView1 = (TextView)findViewById(R.id.textView1); ...

  10. android中底部弹窗,Android实现从底部弹出的Dialog示例(一)

    一.概述 先给大家看一下效果图: 点击中间的显示弹框按钮,从底部弹出来一个对话框,用户可以点击拍照或者从相册选择进行相应的操作,下面看看怎么实现. 二.代码实现 主页面布局文件,很简单,一个按钮,响应 ...

最新文章

  1. 在cuDNN中简化Tensor Ops
  2. 初创公司根本没数据,增长黑客个屁
  3. python下载网页中的pdf文件_【Python】Python的urllib模块、urllib2模块批量进行网页下载文件...
  4. 如何实现Spark on Kubernetes?
  5. python udp客户端 服务器实现方式_python3实现UDP协议的简单服务器和客户端
  6. WordCount处理过程
  7. 【NIPS2020】 Workshop List
  8. jmeter java 关联_使用Jmeter进行数据关联和并发用户
  9. 制造与供应链中的人工智能行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  10. Java 多线程编程两个简单的例子
  11. Idea tomcat控制台日志乱码
  12. 11.05面向对象 封装
  13. 一键调整PCB丝印,超级好用
  14. 两步解决WIN7 下IE经常未响应的假死现象
  15. MCE公司:重磅!明星靶点TLR4助力攻克脑血管难题!
  16. imagine php,使用imagine/imagine实现制作一个图片
  17. 迅捷路由器造成计算机无法上网,迅捷路由器不能上网怎么办
  18. 设计理财产品推荐系统场景
  19. C++ push方法与push_back方法 浅析
  20. 花草茶之常用配方及功效说明

热门文章

  1. 达芬奇剪辑调色专用键盘DaVinci Resolve Speed Editor
  2. 【Opencv卸载与重装】NVIDIA Xavier NX下,卸载opencv3,重装opencv4
  3. VXLAN技术学习笔记
  4. 一个手机阅读器的WebApp
  5. 那些年,我们一起被坑的H5音频
  6. Redis入门学习笔记--附Redis工具类
  7. w10怎么改mysql用户名_win10用户名彻底改为英文的详细操作步骤
  8. 录制软件obs的使用方法
  9. Win10如何设置护眼模式?
  10. Druid连接池原理