android中自定义 toast,Android自定义Toast
在开发中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相关推荐
- Android中导航栏之自定义导航布局
Toolbar系列文章导航 Android中导航栏之Toolbar的使用 Android中导航栏之溢出菜单OverflowMenu Android中导航栏之搜索框SearchView Android中 ...
- Android中如何自己定义吐司(Toast)
Android系统里面有个东西很好用,也很常用,那就是Toast,但是长期使用也会发现,Toast有他的不足之处:形式单一,只有文字,风格不变等等,那么要如何自定义一个Toast呢,我们可以先从分析A ...
- Android中SimpleAdapter的使用—自定义列表
本人初学Android,今天研究到Adapter这块感觉挺有意思的,写了个自定义列表进行测试 首先我们新建一个layout列表布局文件,具体布局可以自己设定. 下面贴上我的自定义布局文件代码 1 &l ...
- Android中实现Bitmap在自定义View中的放大与拖动
一基本实现思路: 基于View类实现自定义View –MyImageView类.在使用View的Activity类中完成OnTouchListener接口,实现对MotionEvent事件的监听与处理 ...
- android 如何去掉自定义标签页,Android中为TextView增加自定义的HTML标签
Android中的TextView,本身就支持部分的Html格式标签.这其中包括常用的字体大小颜色设置,文本链接等.使用起来也比较方便,只需要使用Html类转换一下即可.比如: textView.se ...
- Android中为TextView增加自定义的HTML标签
为什么80%的码农都做不了架构师?>>> Android中的TextView,本身就支持部分的Html格式标签.这其中包括常用的字体大小颜色设置,文本链接等.使用起来也比较方便 ...
- android 中dialog对话框,Android中的对话框dialog
普通对话框 单选对话框 多选对话框 进度条对话框 底部弹出框 1.普通对话框 this 代表当前类 最终继承Context 相当于是子类 getApplicationContext:直接返回的是Con ...
- android中oncreate方法,android开发之onCreate( )方法详解
这里我们只关注一句话:This is where you should do all of your normal static set up.其中我们只关注normal static, normal ...
- Android中list常用方法,Android中的常用控件及其基本用法
TextView的使用方法 布局文件中的配置: 在Activity类中的使用: TextView textView1 = (TextView)findViewById(R.id.textView1); ...
- android中底部弹窗,Android实现从底部弹出的Dialog示例(一)
一.概述 先给大家看一下效果图: 点击中间的显示弹框按钮,从底部弹出来一个对话框,用户可以点击拍照或者从相册选择进行相应的操作,下面看看怎么实现. 二.代码实现 主页面布局文件,很简单,一个按钮,响应 ...
最新文章
- 在cuDNN中简化Tensor Ops
- 初创公司根本没数据,增长黑客个屁
- python下载网页中的pdf文件_【Python】Python的urllib模块、urllib2模块批量进行网页下载文件...
- 如何实现Spark on Kubernetes?
- python udp客户端 服务器实现方式_python3实现UDP协议的简单服务器和客户端
- WordCount处理过程
- 【NIPS2020】 Workshop List
- jmeter java 关联_使用Jmeter进行数据关联和并发用户
- 制造与供应链中的人工智能行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
- Java 多线程编程两个简单的例子
- Idea tomcat控制台日志乱码
- 11.05面向对象 封装
- 一键调整PCB丝印,超级好用
- 两步解决WIN7 下IE经常未响应的假死现象
- MCE公司:重磅!明星靶点TLR4助力攻克脑血管难题!
- imagine php,使用imagine/imagine实现制作一个图片
- 迅捷路由器造成计算机无法上网,迅捷路由器不能上网怎么办
- 设计理财产品推荐系统场景
- C++ push方法与push_back方法 浅析
- 花草茶之常用配方及功效说明