Android自定义Toast

1,553 阅读1分钟

这是一个可以自定义Toast的UI的工具类。废话少说,直接上代码。

package com.newtonapple.zhangyiyan.zhangyiyan.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.newtonapple.zhangyiyan.R;

public class ToastUtils {

//直接显示Toast
public static void show(Context context, String info) {

    View view = getTextView(context, info);
    Toast toast = new Toast(context);
    toast.setView(view);
    toast.show();

}

private static View getTextView(Context context,String info){
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.tv_toast, null);
    TextView tv = (TextView) view.findViewById(R.id.tv_toast);
    tv.setText(info);
    return view;
}

//设置Toast可以显示多长时间
public static void show(Context context, String info, final long duration) {
    if (context == null)
    {
        return;
    }
    final Toast toast = Toast.makeText(context, info, Toast.LENGTH_SHORT);
    toast.show();
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            toast.cancel();
        }
    }, duration);
}

//自定义样式的Toast
public static void showZhengZaiKaiTong(Context context) {
if (context == null)
{
return;
}
final Toast toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
//自定义Toast的布局
View view = LayoutInflater.from(context).inflate(R.layout.layout_toast,null,false);
LinearLayout ll_toash = (LinearLayout) view.findViewById(R.id.toast);

    //布局文件中设置的宽高不顶用,需要重新设置;注意:不能设置最外层控件的宽高,会报空指针,可以设置第二层控件的宽高
    Activity activity = (Activity) context;
    WindowManager windowManager = activity.getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();
    ll_toash.getLayoutParams().width = (int) (screenWidth*0.411);
    ll_toash.getLayoutParams().height = (int) (screenHeight*0.18);

    //设置吐司居中显示
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.setView(view);
    toast.show();
}

//自定义样式的Toast
public static void yiShanChu(Context context) {
    if (context == null)
    {
        return;
    }
    final Toast toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
    View view = LayoutInflater.from(context).inflate(R.layout.layout_toast_delete,null,false);
    LinearLayout ll_toash = (LinearLayout) view.findViewById(R.id.toast);
    //布局文件中设置的宽高不顶用,需要重新设置;注意:不能设置最外层控件的宽高,会报空指针,可以设置第二层控件的宽高
    Activity activity = (Activity) context;
    WindowManager windowManager = activity.getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();
    ll_toash.getLayoutParams().width = (int) (screenWidth*0.411);
    ll_toash.getLayoutParams().height = (int) (screenHeight*0.18);

    //设置吐司居中显示
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.setView(view);
    toast.show();
}

}