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显示的时间是不可控的.我们仅仅能改动他的显示样式和显示的位置,尽管他提供了一个显示时间的设置方法.可是那是没有效果 ...
随机推荐
- C#微信开发回复信息
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;usi ...
- 小知识点 LINQ中延时求值和主动求值的区别
先看个简单的例子: List<, , , , , , }; select c; select c).ToList<int>(); list.Add(); Console.WriteL ...
- JS闭包理解_摘
原文地址1:http://www.cnblogs.com/mzwr1982/archive/2012/05/20/2509295.html 闭包是一个比较抽象的概念,尤其是对js新手来说.书上的解释实 ...
- windows Api AlphaBlend的使用方法
很早的时候公司里开发了个c++客户端软件,需要支持windows2000,要求简单小巧,并且不希望引入其他界面库,图形引擎之类的东西,像gdi+之类的,于是对于美工做出来的一些界面,需要有alpha通 ...
- c#中sqlhelper类的编写(二)
上一篇文章讲了简易版的SqlHelper类的编写,我们在这里就上一篇文章末尾提出的问题写出解决方案. sql语句注入攻击已经是众所周知的了.我们如何在C#中保护自己的数据库不被这样的方式攻击呢? 不用 ...
- OpenStack 加入新的节点,创建虚拟机失败的问题
最开始做OpenStack的时候,由于只是为了部署测试用,因此将所有的部分都装在一台单网卡的机器上,费了九牛二虎之力终于部署成功,其中最主要的两块问题出现在以下两个方面: 1:nova.neutron ...
- windows时间同步出错
http://jingyan.baidu.com/article/fd8044faf1f7ae5030137a7d.html
- UI4_UIWebView
// // ViewController.m // UI4_UIWebView // // Created by zhangxueming on 15/7/7. // Copyright (c) 20 ...
- OC9_代理正向传值
// // ProtectedDeleagate.h // OC9_代理正向传值 // // Created by zhangxueming on 15/6/24. // Copyright (c) ...
- Codevs 1105 过河
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有 ...