Toast 弹出在屏幕中间位置以及自定义

默认Toast:

Toast.makeText(MainActivity.this,"点击",Toast.LENGTH_SHORT).show();

设置Toast位置:

通过setGravity设置Toast位置,可以是 

 Gravity.CENTER:中间

 Gravity.BOTTOM:下方

 Gravity.TOP:上方

 Gravity.RIGHT:右边

Gravity.LEFT:左边

 Toast toast = Toast.makeText(getApplicationContext(), "点击", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();

Toast 也可以是个布局:这个布局里可以是任何控件  图片 文字 等

Toast toast2 = new Toast(MainActivity.this);
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_custom, null);
                toast2.setView(view);
                toast2.setGravity(Gravity.CENTER, 0, 0);
                toast2.show();

 ToastUtil类:

public class ToastUitl {
 
 
    private static Toast toast;
    private static Toast toast2;
 
    /**
     * 初始化Toast(消息,时间)
     */
    private static Toast initToast(CharSequence message, int duration) {
        if (toast == null) {
            toast = Toast.makeText(BaseApplication.getAppContext(), message, duration);
        } else {
            //设置文字
            toast.setText(message);
            //设置存续期间
            toast.setDuration(duration);
        }
        return toast;
    }
 
    /**
     * 短时间显示Toast(消息 String等)
     */
    public static void showShort(CharSequence message) {
        initToast(message, Toast.LENGTH_SHORT).show();
    }
 
 
    /**
     * 短时间显示Toast(资源id)
     */
    public static void showShort(int strResId) {
        initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast.LENGTH_SHORT).show();
    }
 
    /**
     * 长时间显示Toast(消息 String等)
     */
    public static void showLong(CharSequence message) {
        initToast(message, Toast.LENGTH_LONG).show();
    }
 
    /**
     * 长时间显示Toast(资源id)
     */
    public static void showLong(int strResId) {
        initToast(BaseApplication.getAppContext().getResources().getText(strResId), Toast.LENGTH_LONG).show();
    }
 
    /**
     * 自定义显示Toast时间(消息 String等,时间)
     */
    public static void show(CharSequence message, int duration) {
        initToast(message, duration).show();
    }
 
    /**
     * 自定义显示Toast时间(消息 资源id,时间)
     */
    public static void show(int strResId, int duration) {
        initToast(BaseApplication.getAppContext().getResources().getText(strResId), duration).show();
    }
 
    /**
     * 显示有image的toast 这是个view
     */
    public static Toast showToastWithImg(final String tvStr, final int imageResource) {
        if (toast2 == null) {
            toast2 = new Toast(BaseApplication.getAppContext());
        }
        View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.toast_custom, null);
        TextView tv = (TextView) view.findViewById(R.id.toast_custom_tv);
        tv.setText(TextUtils.isEmpty(tvStr) ? "" : tvStr);
        ImageView iv = (ImageView) view.findViewById(R.id.toast_custom_iv);
        if (imageResource > 0) {
            iv.setVisibility(View.VISIBLE);
            iv.setImageResource(imageResource);
        } else {
            iv.setVisibility(View.GONE);
        }
        toast2.setView(view);
        toast2.setGravity(Gravity.CENTER, 0, 0);
        toast2.show();
        return toast2;
 
    }
}

补加:

自定义Toast 填充满整个屏幕:

Toast toast2 = new Toast(MainActivity.this);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_custom, null);
ImageView iv_toast = (ImageView) view.findViewById(R.id.iv_toast);
TextView tv_toast = (TextView) view.findViewById(R.id.tv_toast);
toast2.setView(view);
toast2.setGravity(Gravity.FILL_HORIZONTAL | Gravity.VERTICAL_GRAVITY_MASK, 0, 0);
toast2.show();

注意:布局文件要

android:layout_width="match_parent"
android:layout_height="match_parent"
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值