使用Android默认的Toast

Toast简介:

Toast是一个简单的消息显示框,能够短暂的出现在屏幕的某个位置,显示提示消息。

默认的位置是屏幕的下方正中,一般Toast的使用如下:

 Toast.makeText(this,"1222222",Toast.LENGTH_SHORT).show();

Toast是static修饰的静态类,意味着可以直接使用,所以可以不用创建对象直接调用makeText方法,

该方法需要传入三个参数:

   /**
* Make a standard toast that just contains a text view.
*
* @param context The context to use. Usually your {@link android.app.Application}
* or {@link android.app.Activity} object.
* @param text The text to show. Can be formatted text.
* @param duration How long to display the message. Either {@link #LENGTH_SHORT} or
* {@link #LENGTH_LONG}
*
*/

第一个参赛数当前context,第二个是需要显示的文本内容,第三个参数是显示时间

但这里的显示时间只有两种,一个是 Toast.LENGTH_SHORT 和 Toast.LENGTH_LONG. 顾名思义,后者比前者要长一点。

自定义Toast

自定义图片

今天看到某音乐播放软件有个收藏功能会弹出类似效果的Toast

上面一颗红♥️,下面显示文本内容, 那么这个效果如何实现呢?

在打开Toast 源码可以看到一个方法setView

   /**
* Set the view to show.
* @see #getView
*/
public void setView(View view) {
mNextView = view;
}

想必可以通过该方法添加图片和文本

那接下来就可以尝试自定义一个布局文件,并把该布局通过setView的方式添加到Toast里面

布局文件为线型布局,内容如下,添加一个现形布局,在该线型布局中添加一个ImageView和一个TextView

该布局文件名为toast_view.xml,设置orientation为vertical为垂直排列,并将准备好的心型图片设置为ImageView的背景

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:gravity="center"
android:minWidth="100dp"
android:orientation="vertical">
<!--android:background="@drawable/toast_bg"-->
<ImageView
android:id="@+id/toast_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_margin="2dp"
android:background="@drawable/redheart" /> <TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:gravity="center"
android:text=""
android:textColor="#ffffff"
android:textSize="15dp" />
</LinearLayout> </LinearLayout>

结下来创建一个ToastView Class,把该布局文件关联起来

   /**
*
* @param context
* @param text
*/
public ToastView(Context context, String text) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.toast_view, null);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
if (toast != null) {
toast.cancel();
}
toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
}

通过setText方法把要显示的文本显示出来

当然还可以进一步优化,把ImageView的背景替换掉

 public ToastView(Context context, String text) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.toast_view, null);
ImageView imageView=(ImageView)view.findViewById(R.id.toast_image);
imageView.setBackgroundResource(R.mipmap.ic_launcher);
TextView t = (TextView) view.findViewById(R.id.toast_text);
t.setText(text);
if (toast != null) {
toast.cancel();
}
toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
}

通过这个方法,先获取到Layout然后通过findViewById获取到子控件进行设置

然而这样的效果依然不是我们想要的,显示出来并不是带圆角的

这个时候就需要添加一个shape布局

设置圆角,并把该shape添加到LinearLayout的背景

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#c83e3e3e" /> <!--radius shape-->
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:radius="8dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>

自定义位置

那么如何自定义显示位置?

通过查看Toast的源码可以看到一个setGravity的方法,是专门用来设置Toast的位置

   /**
* Set the location at which the notification should appear on the screen.
* @see android.view.Gravity
* @see #getGravity
*/
public void setGravity(int gravity, int xOffset, int yOffset) {
mTN.mGravity = gravity;
mTN.mX = xOffset;
mTN.mY = yOffset;
}

该方法有三个参赛,第一个是整形类型的gravity,该参数设置具体的位置,可以参考Gravity类

一般常用的有:

Gravity.CENTER
Gravity.LEFT
Gravity.RIGHT
Gravity.TOP
Gravity.BOTTOM

顾名思义,第二个和第三个参数是偏移量,针对第一个参数的偏移量

所以,如果设置Toast在屏幕正当中,只需要这样

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

自定义Toast的显示时间

未完待续。。。。。。

Android带图片的Toast(自定义Toast)的更多相关文章

  1. Android 自定义Android带图片和文字的ImageButton

    经过分析,上述按钮效果实际上就是一个布局,一个最简单不过的垂直线性布局,上部分是一个ImageView,下部分是一个TextView,这个布局可点击.可设置监听. 我们首先要编写自己的ImageBut ...

  2. 旋转toast 自定义toast方向,支持多个方向的显示,自定义View

    package com.example.canvasdemo; import java.security.InvalidAlgorithmParameterException; import andr ...

  3. Android圆形图片不求人,自定义View实现(BitmapShader使用)

    在很多APP当中,圆形的图片是必不可少的元素,美观大方.本文将带领读者去实现一个圆形图片自定View,力求只用一个Java类来完成这件事情. 一.先上效果图 二.实现思路 在定义View 的onMea ...

  4. 【Android代码片段之六】Toast工具类(实现带图片的Toast消息提示)

    转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/6841266  作者:张燕广 实现的Toast工具类ToastUtil封装 ...

  5. Android开发之自定义Toast(带详细注释)

    因为工作需求,所以自己研究了自定义Toast,这里做出总结: 在此之前有一点需要提前说明:Toast与其他组件一样,都属于UI界面中的内容,因此在子线程中无法使用Toast弹出提示内容,如果强行在子线 ...

  6. Android开发系列(二十三):实现带图片提示的Toast提示信息框

    Android中的Toast是非经常见的一个消息提示框.可是默认的消息提示框就是一行纯文本.所以我们能够为它设置一些其它的诸如是带上图片的消息提示. 实现这个非常easy: 就是定义一个Layout视 ...

  7. Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明

    http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...

  8. 显示图片的(自定义)吐司Toast

    一般我们提示的时候都是直接提示文字的,其实Toast也可以显示图片 常用方法 Toast.makeText(context,text,duration)这返回一个Toast对象 toast.setDu ...

  9. Android 自定义Toast

    自定义Toast 其实就是自定义布局文件 感觉利用Dialog或者PopupWindow做也差不多 上图上代码 public class MainActivity extends Activity { ...

随机推荐

  1. php杂项

    php5.3新增闭包函数用法use用法(引入变量地址且随内存中值变化而变化,跳过解析顺序直接获取函数最终值) $obj = (object) "Hello, everyone"; ...

  2. ABP文档 :Overall - Module System

    模块介绍 ABP提供了构建模块并将这些模块组合起来创建应用的基础设施.一个模块可以依赖另一个模块.一般来说,一个程序集可以认为是一个模块.如果应用中有多个程序集,建议为每个程序集创建一个模块定义.模块 ...

  3. android-配置文件AndroidManifest.xml

    AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activities, services, 等等),他们各自的实 ...

  4. Xftp连接linux(ubuntu)时提示ssh服务器拒绝了密码,请再试一次

    用xftp root连接时显示ssh服务器拒绝了密码,请重新连接.由于sshd的设置不允许root用户用密码远程登录,修改/etc/ssh/sshd_config文件,但必须是安装了openssh才会 ...

  5. 浅析 Android 的窗口

    来源:http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=555&fromuid=6   一.窗口的概念 在开发过程中,我们经常会 ...

  6. IIS配置MP3/MP4/OGG/flv等资源文件访问

    配置过程参考:http://www.cnblogs.com/EasonJim/p/4752399.html 以下包含了mp4的mime类型: 323 text/h323 acx application ...

  7. 8 HTML&JS等前端知识系列之jquery的自定义方法

    preface 有时候我们在前端写jquery的时候,会自己自定义些方法,这样可以不需要重复造轮子.先说说2种自定义方法的区别: 不跟在选择器后面的 跟在选择器后面的. 那下面说说如何自定义jquer ...

  8. js 去除字符串中间的空格

    function trims(str){ return str.replace(/[ ]/g,""); //去除字符串中间的空格 }

  9. PHP中的全局变量global和$GLOBALS的区别

    1.global Global的作用是定义全局变量,但是这个全局变量不是应用于整个网站,而是应用于当前页面,包括include或require的所有文件. 但是在函数体内定义的global变量,函数体 ...

  10. Adapt适配器

    为接口提供所有的空实现,让使用者仅仅覆盖需要的部分: 本思想肯定有问题,慢慢考究 frame.addWindowListener(new WindowAdapter() { @Override pub ...