Android 自定义Toast,不使用系统Toast
效果图:

创建Toast类
package com.example.messageboxtest; import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
*
* @author chaowen
*
*/
public class MyMsgBox { private static final int ANIMATION_DURATION = 600; private int HIDE_DELAY = 5000; private View mContainer; private int gravity = Gravity.CENTER; private TextView mTextView; private Handler mHandler; private AlphaAnimation mFadeInAnimation; private AlphaAnimation mFadeOutAnimation; public MyMsgBox(Context context, int HIDE_DELAY, int gravity) {
ViewGroup container = (ViewGroup) ((Activity) context)
.findViewById(android.R.id.content);
View v = ((Activity) context).getLayoutInflater().inflate(
R.layout.newmb__messagebar, container);
this.HIDE_DELAY = HIDE_DELAY;
this.gravity = gravity;
init(v);
} private void init(View v) {
mContainer = v.findViewById(R.id.mbContainer);
mContainer.setVisibility(View.GONE);
mTextView = (TextView) v.findViewById(R.id.mbMessage);
mFadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
mFadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
mFadeOutAnimation.setDuration(ANIMATION_DURATION);
mFadeOutAnimation
.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
} @Override
public void onAnimationEnd(Animation animation) {
mContainer.setVisibility(View.GONE);
} @Override
public void onAnimationRepeat(Animation animation) {
}
}); mHandler = new Handler(); } public void show(String message) {
mContainer.setVisibility(View.VISIBLE); ((LinearLayout) mContainer).setGravity(gravity
| Gravity.CENTER_VERTICAL); mTextView.setText(message); mFadeInAnimation.setDuration(ANIMATION_DURATION); mContainer.startAnimation(mFadeInAnimation);
mHandler.postDelayed(mHideRunnable, HIDE_DELAY);
} private final Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
mContainer.startAnimation(mFadeOutAnimation);
}
}; }
对应的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mbContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:gravity="bottom"
android:orientation="vertical" > <LinearLayout
style="@style/bgTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="bottom"
android:orientation="vertical" > <TextView
android:id="@+id/mbMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test"
android:textColor="@drawable/white" />
</LinearLayout> </LinearLayout>
使用方法:
MyMsgBox m = new MyMsgBox(arg0.getContext(), 5000,
Gravity.BOTTOM);
m.show("这是一个性化Toast");
Android 自定义Toast,不使用系统Toast的更多相关文章
- android自定义风格的toast
先上图看一下我的自定义toast的样式 源码下载地址: CustomToastActivity.java源码 package com.jinhoward.ui_customtoast; /** * A ...
- 023 Android 自定义Toast控件
1.Toast自定义控件工具类 package com.example.administrator.test62360safeguard.Utils; import android.content.C ...
- Android Toast 封装,避免Toast消息覆盖,替换系统Toast最好用的封装
Android Toast 封装,避免Toast消息覆盖,无阻塞,等强大功能 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- Android自定义日历控件(继承系统控件实现)
Android自定义日历控件(继承系统控件实现) 主要步骤 编写布局 继承LinearLayout设置子控件 设置数据 继承TextView实现有圆圈背景的TextView 添加Attribute 添 ...
- Android自定义系统分享面板
在Android中实现分享有一种比较方便的方式,调用系统的分享面板来分享我们的应用.最基本的实现如下: public Intent getShareIntent(){ Intent intent = ...
- Android界面设计之对话框——定制Toast、AlertDialog
一.概述 在界面设计中需要根据用户操作显示提示信息.出错信息等,就要用到对话框.Android实现提示信息显示常用有两种方式 1.Toast 2.AlertDialog 二.Toast Android ...
- <Android 基础(十六)> Toast
介绍 A toast provides simple feedback about an operation in a small popup. It only fills the amount of ...
- Android学习(十八)Toast的使用
一.什么是Toast 1.Toast是一种提供给用户简洁提示信息的视图. 2.该视图以浮于应用程序之上的形式呈现给用户, Toast提示界面不获取焦点,在不影响用户使用的情况下,给用户某些提示. 3. ...
- Android中通过反射来设置Toast的显示时间
这个Toast的显示在Android中的用途还是非常大的,同一时候我们也知道toast显示的时间是不可控的.我们仅仅能改动他的显示样式和显示的位置,尽管他提供了一个显示时间的设置方法.可是那是没有效果 ...
随机推荐
- MongoDB - The mongo Shell, Access the mongo Shell Help
In addition to the documentation in the MongoDB Manual, the mongo shell provides some additional inf ...
- C++类(一)
#include<iostream> #include<string> using namespace std; //找了很久,似乎都是类放在Main方法体上 class Pe ...
- iOS 简单block的使用
1.第一种方法 声明block: - (void)test:(int) param_1 completion:(void(^)(int)) completion; 实现block: -(void)te ...
- 如何使用10个小时搭建出个人域名而又Geek的独立博客?
1.安装准备软件 Node.js.Git.GitHub DeskTop(前两个必须安装,后者可选) 2.本地搭建hexo框架.配置主题.修改参数.实现本地测试预览 3.链接GitHub.实现在线预览 ...
- JAVA JDBC 元数据分析小结
纯干货: 获取数据库名称: /** * 获取数据库的名称 */ public void getDataBaseName() throws Exception { Connection con = DS ...
- 原生js实现addClass,removeClass,hasClass方法
function hasClass(elem, cls) { cls = cls || ''; if (cls.replace(/\s/g, '').length == 0) return false ...
- jquery里用each遍历的值存到数组和字符串
$("img").each(function(){ var a = $(this).attr("src"); }); //遍历后存放到数组中..要用的时候再根据 ...
- 7 款令人赞叹的 HTML5 动画应用
1.HTML5 3D图片阴影翻转动画 这是一款很酷的HTML5 3D动画特效,这款3D特效可以为你的图片增加阴影的效果,而且可以让图片在鼠标滑过的时候出现3D翻转的动画效果. 在线演示 源码下载 2. ...
- log4j自动生成日志文件配置
生成文件到默认位置: #生成日志文件 #log4j.appender.systemFile=org.apache.log4j.RollingFileAppender #按天生成 log4j.appen ...
- 【Qt】Qt之进程间通信(IPC)【转】
简述 进程间通信,就是在不同进程之间传播或交换信息.那么不同进程之间存在着什么双方都可以访问的介质呢?进程的用户空间是互相独立的,一般而言是不能互相访问的,唯一的例外是共享内存区.但是,系统空间却是“ ...