Android-重新包装Toast,自定义背景

Android-重新包装Toast,自定义背景

2016-4-27
Android L

算是包装了一个自己使用的小工具。
使用Toast的目的是弹一个提示框。先看一下Toast.makeText方法。

Toast.makeText(getApplicationContext(), this, "弹出一个Toast", Toast.LENGTH_SHORT).show();

使用了Android自己的一个layout,然后把传入的text放到layout的TextView中。

public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
    Toast result = new Toast(context);

    LayoutInflater inflate = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
    TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
    tv.setText(text);

    result.mNextView = v;
    result.mDuration = duration;

    return result;
}

我们想自定义个Toast背景,并且调用方法和原来的Toast类似。
观察一下上面的Toast,使用的是Android里面的View。在这一步把View改成我们自己的即可达到目的。
新建文件ToastCustom.java

public class ToastCustom {

    public static final int LENGTH_SHORT = Toast.LENGTH_SHORT;
    public static final int LENGTH_LONG = Toast.LENGTH_LONG;

    Toast toast;
    Context mContext;
    TextView toastTextField;

    public ToastCustom(Context context, Activity activity) {
        mContext = context;
        toast = new Toast(mContext);
        toast.setGravity(Gravity.BOTTOM, 0, 260);// 位置会比原来的Toast偏上一些
        View toastRoot = activity.getLayoutInflater().inflate(R.layout.toast_view, null);
        toastTextField = (TextView) toastRoot.findViewById(R.id.toast_text);
        toast.setView(toastRoot);
    }

    public void setDuration(int d) {
        toast.setDuration(d);
    }

    public void setText(String t) {
        toastTextField.setText(t);
    }

    public static ToastCustom makeText(Context context, Activity activity, String text, int duration) {
        ToastCustom toastCustom = new ToastCustom(context, activity);
        toastCustom.setText(text);
        toastCustom.setDuration(duration);
        return toastCustom;
    }

    public void show() {
        toast.show();
    }
}

新建一个layout toast_view.xml,里面的style自己定义

<?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:orientation="vertical">

    <TextView
        android:id="@+id/toast_text"
        style="@style/TextToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/shape_toast"
        android:textColor="@color/white" />
</LinearLayout>

调用方式和Toast一样:

ToastCustom.makeText(getApplicationContext(), this, "弹出自定义背景Toast",
                        ToastCustom.LENGTH_SHORT).show();

之后就可以很方便地使用这个自定义背景的Toast

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值