• 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");

     

  • 相关阅读:
    log4net 发布到生产环境不写日志的解决方法--使用 NLog日志
    centos 下Supervisor 守护进程基本配置
    centos 7 下安装Nginx
    Haproxy+asp.net +RedisSessionStateProvider 完美实现负载均衡,并且session保持
    centos之Haproxy 负载均衡学习笔记
    改进初学者的PID-介绍
    实现Modbus TCP多网段客户端应用
    有一种亲切是手机
    实现Modbus ASCII多主站应用
    爱好
  • 原文地址:https://www.cnblogs.com/andgoo/p/3157691.html
Copyright © 2020-2023  润新知