自定义Toast

本文介绍了一种自定义Toast的方法,通过创建ToastView类,可以设置Toast的显示时间、位置等属性,并提供了示例代码。

   用系统的Toast提示,停留时间太短了,不满足某些应用的需求。有时候希望这个Toast能停留久一点,或者让这个Toast居中显示,所以,自己定义了一个Toast。直接上代码了:

package com.changewei.testvolleyandimageloader;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 自定义Toast
 * @author chenlin
 *
 */
public class ToastView {
	
	public static Toast toast;
	private int time;
	private Timer timer;
	/**
	 * 构造函数,默认显示时间Toast.LENGTH_SHORT
	 * @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);
	}
	/**
	 * 构造函数,默认显示时间Toast.LENGTH_SHORT
	 * @param context
	 * @param text
	 */
	public ToastView(Context context, int 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);
	}
	
	//设置toast显示位置
	public void setGravity(int gravity, int xOffset, int yOffset) {
		//toast.setGravity(Gravity.CENTER, 0, 0); //居中显示
		toast.setGravity(gravity, xOffset, yOffset);
	}
	
	//设置toast显示时间
	public void setDuration(int duration) {
		toast.setDuration(duration);
	}
	
	//设置toast显示时间(自定义时间)
	public void setLongTime(int duration) {
		//toast.setDuration(duration);
		time = duration;
		timer = new Timer();
        timer.schedule(new TimerTask(){
        	@Override
        	public void run() {
        		 
        		if(time-1000 >= 0) {
        			show();
        			time= time - 1000;
        		} else {
        			timer.cancel();
        		}
        	}
        }, 0, 1000);
	}
	
	public void show() {
		toast.show();
	}
	
	public static void cancel() {
		if(toast != null) {
			toast.cancel();
		}
	}

}


xml文件:

 

<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="@drawable/toast_bg"
        android:gravity="center"
        android:minWidth="120dp"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_margin="10dp"
            android:background="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/toast_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text=""
            android:textColor="#ffffff"
            android:textSize="15dp" />
    </LinearLayout>

</LinearLayout>



toast_bg文件:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#c8333333" />

    <corners
        android:bottomLeftRadius="6dp"
        android:bottomRightRadius="6dp"
        android:topLeftRadius="6dp"
        android:topRightRadius="6dp" />
            
</shape>
   


 

最后为这个自定义Toast的使用:

 

	ToastView toast = new ToastView(this,"欢迎使用自定义Toast");
            toast.setGravity(Gravity.CENTER, 0, 0);//设置Toast的位置
            toast.setLongTime(5000);//设置Toast显示的时间
            toast.show();//设置Toast显示
//            toast.cancel();//取消Toast


 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值