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");
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探