android自定义toast

首先给toast一个布局view

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="#0a0084FD">

    <TextView
        android:id="@+id/toastText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="16sp" />
</FrameLayout>
toast正文代码部分,创建一个自己的toast类

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

import honghu.com.hong_hu.R;


/**
 * Created by Administrator on 2017/5/12.
 */

public class MyToast {
    private Toast mToast;

    /**
     * @param context  一个上下文对象
     * @param text     toast的文本内容
     * @param duration toast的时间显示
     *                 在构造方法中将自定义toast进行初始化
     */
    public MyToast(Context context, CharSequence text, int duration) {
        View view = LayoutInflater.from(context).inflate(R.layout.mytoast_view, null);
        TextView toastText = (TextView) view.findViewById(R.id.toastText);
        mToast = new Toast(context);
        toastText.setText(text);
        mToast.setDuration(duration);
        mToast.setView(view);
    }

    public static MyToast makeText(Context context, CharSequence text, int duration) {
        return new MyToast(context, text, duration);
    }

    public void show() {
        if (mToast != null) {
            mToast.show();
        }
    }

    public void setGravity(int gravity, int xOffset, int yOffset) {
        mToast.setGravity(gravity, xOffset, yOffset);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值