Android自定义Toast

本文介绍了一个用于自定义Android Toast样式的工具类,包括如何创建、设置显示时长及自定义布局。

这是一个可以自定义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();
}复制代码

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值