自定义toast的布局

今天要做个比较简单的需求,就是类似toast的弹出框,说它是弹出框,美工说了,3-5秒要给我消失。。。gg,不可能,定时器什么的搞起来。我靠,这不就是个toast吗。度娘了一下,果然可以自定义toast的布局。哟西,一波带走。

这里写图片描述

一、xml文件。这里我的外层又嵌套了一层看视无关紧要的LinearLayout(viewgroup)。但是如果不加它的话,你会gg的,具体的我不解释。请移步这里http://www.2cto.com/kf/201407/313054.html

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <LinearLayout
        android:layout_width="378px"
        android:layout_height="192px"
        android:background="@drawable/shape_gettrue"
        android:gravity="center">

        <TextView
            android:id="@+id/tv_toastcontent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:drawablePadding="28px"
            android:drawableTop="@drawable/icon_right"
            android:text="修改成功!"
            android:textColor="@color/white"
            android:textSize="28px" />

    </LinearLayout>
</LinearLayout>

二、封装。赠送几个。。。郭神的微技巧,就是不会重复弹出toast…

package com.coofond.carservices.utils;

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

import com.coofond.carservices.R;

public class ToastUtil {
    /**
     * @author zsj
     * @descraption 点击多次如果当前toast已经存在不再弹出。Toast.LENGTH_SHORT=2s,Toast.LENGTH_LONG=3.5s
     * @Date 2016/8/12
     */
    private static Toast mToast;
    private static Toast mImgToast;

    /**
     * toast通知
     *
     * @param mContext
     * @param str
     */
    public static void toastCenter2(final Context mContext, final String str) {
        if (mToast == null) {
            mToast = Toast.makeText(mContext, str, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(str);
        }
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.show();
    }

    /**
     * toast通知
     *
     * @param mContext
     * @param str
     */
    public static void toastBottom2(final Context mContext, final String str) {
        if (mToast == null) {
            mToast = Toast.makeText(mContext, str, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(str);
        }
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.show();
    }

    /**
     * toast通知
     *
     * @param mContext
     * @param str
     */
    public static void toastBottom3(final Context mContext, final String str) {
        if (mToast == null) {
            mToast = Toast.makeText(mContext, str, Toast.LENGTH_LONG);
        } else {
            mToast.setText(str);
        }
        mToast.setGravity(Gravity.BOTTOM, 0, 0);
        mToast.show();
    }

    /**
     * toast通知
     *
     * @param mContext
     * @param str
     */
    public static void toastCenter3(final Context mContext, final String str) {
        if (mToast == null) {
            mToast = Toast.makeText(mContext, str, Toast.LENGTH_LONG);
        } else {
            mToast.setText(str);
        }
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.show();
    }
    /**
     * @param mContext 上下文
     * @param str      内容
     */
    public static void toastCenterwithImg(final Context mContext, final String str) {
            View mView = LayoutInflater.from(mContext).inflate(R.layout.toast_gettrue, null);
            TextView text = (TextView) mView.findViewById(R.id.tv_toastcontent);
            text.setText(str);
            mImgToast = new Toast(mContext);
            mImgToast.setView(mView);
            mImgToast.setGravity(Gravity.CENTER, 0, 0);
            mImgToast.setDuration(Toast.LENGTH_SHORT);
            mImgToast.show();
    }
}

三、使用。就是这么简单

  ToastUtil.toastCenterwithImg(MainActivity.this, "修改成功!");

总结:第一眼看可能会用dialog,然后背景色是那种半透明的,可能会用popupwindow,然而还要3-5秒消失,妥妥的用toast。然而你真的可以用。哈哈,简单实用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值