自定义Toast

系统自带的Toast已能满足绝大部分需求,然鹅在显示时长方面如果有一些要求的话就需要简单进行自定义Toast处理:

首先一个简单的Toast工具类:

 

public class ToastUtils {

    private static Toast toast;

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showShort(Context context, String message) {
        if (TextUtils.isEmpty(message)) {
            return;
        }

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // 自定义布局
        View view = inflater.inflate(R.layout.toast_center, null);
        // 自定义toast文本
        TextView mTextView = (TextView) view.findViewById(R.id.toast_msg);
        mTextView.setText(message + "");

        if (toast == null) {
            toast = new Toast(context);
        }
        toast.setView(view);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM, 0, 200);
        toast.show();
        canCel(toast);

    }

    private static void canCel(final Toast toast) {
        canCel(toast, 900);
    }

    private static void canCel(final Toast toast, int time) {
        Observable.timer(time, TimeUnit.MILLISECONDS).subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {
                if (toast != null)
                    toast.cancel();
            }
        });
    }

}

附上布局代码:

toast_center.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <RelativeLayout
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rl_bg"
        >

        <TextView
            android:text="haha"
            android:id="@+id/toast_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="5dp"
            android:textColor="#ffffff"
            android:textSize="16sp" />
    </RelativeLayout>

</RelativeLayout>

drawable文件:rl_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="5dp"></corners>
    <solid android:color="#99222222"></solid>

</shape>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值