Toast 我想我们应该使用的都很多,一般我们使用默认设置较多,但是默认设置往往不能满足我们的需求,那我们现在来自定义下:

默认Toast:

Toast.makeText(MainActivity.this,"点击按钮",Toast.LENGTH_SHORT).show();

设置Toast位置:

通过setGravity设置Toast位置,可以是

Gravity.CENTER:中间

Gravity.BOTTOM:下方

Gravity.TOP:上方

Gravity.RIGHT:右边

Gravity.LEFT:左

Toast toast = Toast.makeText(getApplicationContext(), "点击按钮", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER, 0, 0);

toast.show();

Toast 也可以是个布局:这个布局里可以是任何控件  图片 文字 等

Toast toast2 = new Toast(MainActivity.this);

View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_custom, null);

toast2.setView(view);

toast2.setGravity(Gravity.CENTER, 0, 0);

toast2.show();

下面附上一个 ToastUtil类:

public class ToastUitl {

private static Toast toast;

private static Toast toast2;

/**

* 初始化Toast(消息,时间)

*/

private static Toast initToast(CharSequence message, int duration) {

if (toast == null) {

toast = Toast.makeText(BaseApplication.getAppContext(), message, duration);

} else {

//设置文字

toast.setText(message);

//设置存续期间

toast.setDuration(duration);

}

return toast;

}

/**

* 短时间显示Toast(消息 String等)

*/

public static void showShort(CharSequence message) {

initToast(message, Toast.LENGTH_SHORT).show();

}

/**

* 短时间显示Toast(资源id)

*/

public static void showShort(int strResId) {

initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast.LENGTH_SHORT).show();

}

/**

* 长时间显示Toast(消息 String等)

*/

public static void showLong(CharSequence message) {

initToast(message, Toast.LENGTH_LONG).show();

}

/**

* 长时间显示Toast(资源id)

*/

public static void showLong(int strResId) {

initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast.LENGTH_LONG).show();

}

/**

* 自定义显示Toast时间(消息 String等,时间)

*/

public static void show(CharSequence message, int duration) {

initToast(message, duration).show();

}

/**

* 自定义显示Toast时间(消息 资源id,时间)

*/

public static void show(int strResId, int duration) {

initToast(BaseApplication.getAppContext().getResources().getText(strResId), duration).show();

}

/**

* 显示有image的toast 这是个view

*/

public static Toast showToastWithImg(final String tvStr, final int imageResource) {

if (toast2 == null) {

toast2 = new Toast(BaseApplication.getAppContext());

}

View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.toast_custom, null);

TextView tv = (TextView) view.findViewById(R.id.toast_custom_tv);

tv.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr);

ImageView iv = (ImageView) view.findViewById(R.id.toast_custom_iv);

if (imageResource > 0) {

iv.setVisibility(View.VISIBLE);

iv.setImageResource(imageResource);

} else {

iv.setVisibility(View.GONE);

}

toast2.setView(view);

toast2.setGravity(Gravity.CENTER, 0, 0);

toast2.show();

return toast2;

}

}

对应的布局文件:此布局文件根据自己需求自定义

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="10dp"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:orientation="vertical">

android:layout_marginTop="10dp"

android:id="@+id/toast_custom_iv"

android:layout_width="60dp"

android:layout_height="60dp"

android:src="@mipmap/ic_launcher"

android:layout_gravity="center"/>

android:id="@+id/toast_custom_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="5dp"

android:textSize="16sp"

android:layout_marginLeft="5dp"

android:layout_marginRight="5dp"

android:layout_marginBottom="10dp"

android:textColor="#000"

tools:text="点击toast" />

补充:

自定义Toast 填充满整个屏幕:

Toast toast2 = new Toast(MainActivity.this);

View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_custom, null);

ImageView iv_toast = (ImageView) view.findViewById(R.id.iv_toast);

TextView tv_toast = (TextView) view.findViewById(R.id.tv_toast);

toast2.setView(view);

toast2.setGravity(Gravity.FILL_HORIZONTAL | Gravity.VERTICAL_GRAVITY_MASK, 0, 0);

toast2.show();

注意:布局文件要

android:layout_width="match_parent"android:layout_height="match_parent"

android toast居中显示_android Toast 弹出在屏幕中间位置以及自定义Toast相关推荐

  1. html弹出窗口怎么居中显示,HTML页面弹出居中可拖拽的自定义窗口层

    使用DIV弹出窗口来动态显示内容的原理:首先采用CSS和HTML隐藏弹窗中的内容,然后利用JavaScript(本教程中是JQuery)来动态显示它们.这种效果不仅能够充分利用有限的版面空间,而且能够 ...

  2. android toast居中显示_Android和iOS7的差异点!

    之前在北京望京SOHO的mou公司做设计一直是iOS,适配Android,所以在做设计时,大部分使用的移动设备是iPhone,这样针对于中型企业: 而后来在一家智能体感设备创业型公司,目标人群是二.三 ...

  3. Android监听系统输入法键盘弹出显示与隐藏事件

    Android监听系统输入法键盘弹出显示与隐藏事件 有时候需要监听Android系统输入法的弹出显示事件,比如:微信聊天时,不管你当前在聊天中的什么位置(上滑查看消息历史),每当你点击输入框时,都会自 ...

  4. Android EditText获取焦点后只显示光标不弹出软键盘

    Android EditText获取焦点后只显示光标不弹出软键盘 参考博客链接 直接说结论 如果你项目适配的最小版本大于等于21 //直接调用EditText的该方法,设置为false即可 bindi ...

  5. android 浮层菜单弹出,Android PopupWindow实现微信右上角的弹出菜单

    日常开发过程中对于PopupWindown的使用也是比较多的.这里给大家展示一下PopupWindow的使用. 修改activity_main.xml布局: xmlns:tools="htt ...

  6. AJAX设计模式之Lightbox---图片和网页外、连 flash、影片都可以显示在 Lightbox 弹出的方框里面

    ========================================================== 什么是Lightbox?看了下面的例子你就会明白: http://www.cnbr ...

  7. 三个sata硬盘只能读取两个_两个SATA硬盘驱动器显示为可弹出

    Siegelion 2月12日的<围攻狮子> 主板也可以自然弹出硬盘变成可弹出,只需习惯一下即可 [在更远的著作(ξαγτηεγ)中提到: ] : 尝试之后,它确实会弹出,这真的很不舒服. ...

  8. Android深入浅出系列之实例应用—弹出消息Toast对象的使用自定义方式(二)

    阅读目录 一:前言 二:实例 三:运行效果 一:前言 Android中定义了一个Toast对象,用以弹出一个窗口来给予用户帮助和提示,和对话框不同的是,Toast并不是以独占方式显示的,它并不会抢夺用 ...

  9. 快速找到message toast弹出的application代码位置

    show Created by Jerry Wang, last modified on Jul 14, 2015 在MessageToast的show方法里设置断点: re-launch appli ...

最新文章

  1. Ubuntu Server下配置UTF-8中文/英文环境
  2. python编程入门指南 代码库在哪下-致Python初学者 Anaconda入门使用指南完整版
  3. python官方网站地址-一些python牛人地址分享
  4. jQuery中HTML的操作
  5. 一文全解析——APP版本管理基本知识
  6. 【算法学习笔记】二叉树的基本操作实现和应用举例,根据先序与中序遍历建立二叉树的实现
  7. 动态规划训练23 [Making the Grade POJ - 3666 ]
  8. 大学生体测成绩判断c语言_体育改革瞄准高校,体测不过关可能真的毕不了业了...
  9. Bezier(贝塞尔)曲线的轨迹规划在自动驾驶中的应用(二)
  10. efl是什么意思_efl
  11. 国庆档电影《我和我的祖国》3天票房破10亿
  12. 微信小程序记录v1.0
  13. java与数据库连接odbc_详解java数据库连接之JDBC-ODBC桥连方式
  14. 人文社科开放数据库汇总
  15. try固定搭配_【昂立推荐】初中英语动词固定搭配大全,值得收藏!
  16. Spring RestTemplate的Basic Auth使用
  17. [ Arduino+Python ] 做一个串口屏,显示CPU使用率
  18. 任意装修、在线DIY定制商城系统,跟紧时代的潮流
  19. 小白记录——识别RNA编辑位点
  20. uniapp实现app跳转app

热门文章

  1. java 中文转成_Java将中文转换成unicode字符。
  2. 8条职场工作经验总结——在 12 家创业公司干过之后
  3. Zynq UltraScale+ MPSoC智能视频平台1:Camera Link接收IP
  4. Shell脚本编程入门
  5. Codeforces 1086 简要题解
  6. Revit二次开发有几种方式?做Revit二次开发的必看!
  7. 机器学习 EM算法 从极大似然法谈起
  8. android打包apk不能安装程序,Android Studio 打包apk后应用未安装
  9. doris insert数据时出现问题:Invalid floating-point literal
  10. Windows 7 英文版操作系统中文软件乱码解决方法