...">
美文网首页
自定义toast

自定义toast

作者: _嘿嘿_ | 来源:发表于2018-04-24 12:06 被阅读0次

1 layout_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="340dp"
android:layout_height="100dp"
android:gravity="center_vertical"
android:padding="13dp"
android:orientation="vertical" >

<TextView
    android:id="@+id/toast_text"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:textColor="#FFDDDDDD"
    android:background="#0ADDDDDD"
    android:gravity="center"
    android:text="自定义"
    android:textSize="26sp" />

</LinearLayout>

2.MyToastUtil.java
package com.geely.util;

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

import com.geely.main.R;

public class MyToastUtil {
private Toast mToast;
TextView textView;
int mDuration;
public @interface Duration {}
private MyToastUtil(Context context, @StringRes int resId, int duration) {
View v = LayoutInflater.from(context).inflate(R.layout.layout_toast, null);
textView = (TextView) v.findViewById(R.id.toast_text);
textView.setText(resId);
mToast = new Toast(context);
mToast.setDuration(duration);
mToast.setView(v);
}

public static MyToastUtil makeText(Context context, @StringRes int resId, int duration) {
    return new MyToastUtil(context, resId, duration);
}
public void show() {
    if (mToast != null) {
        mToast.show();
    }
}
public void setGravity(int gravity, int xOffset, int yOffset) {
    if (mToast != null) {
        mToast.setGravity(gravity, xOffset, yOffset);
    }
}
public void setText(@StringRes int resId) {
    textView.setText(resId);
}
public void setDuration(@Duration int duration) {
    mDuration = duration;
}

}

3.使用
private MyToastUtil mytoast = null;
if (mytoast != null) {
mytoast.setText(R.string.noUpdate);
mytoast.show();
} else {
mytoast = mytoast.makeText(InfoActivity.this,R.string.noUpdate, Toast.LENGTH_SHORT);
mytoast.show();
}

相关文章

  • Toast(自定义Toast)

    默认的Toast: 自定义Toast(自定义图标样式\显示时间\位置): 效果图: 代码:

  • Toast和AlertDialog通用工具

    (1)自定义Toast (2)自定义对话框 (3)二合一 这里涉及到Toast, 使用Toast之前必须先判断是否...

  • Apicloud功能模块使用customToast

    /*Title: customToastDescription: 自定义Toast,让Toast更完美的融入应用,...

  • Tween实战-Toast动画

    自定义Toast 这里并不是创建一个自定义Toast,而是把toast添加到一个window中,对这个window...

  • Android——Toast

    三、使用 1.Toast使用技巧 Toast使用技巧 (1)自定义 Toast 布局 (2)使用 Applicat...

  • Android 自定义Toast

    Android自定义Toast Toast的基础用法 Toast显示的位置通常情况下Toast显示在整个界面的底部...

  • ToastUtil工具类

    自定义toast,解决系统toast连续弹出,长时间不消失的问题

  • Android 带图片的Toast

    Android 带图片的Toast Github代码 带图片的Toast,其实就是自定义布局的Toast,只要T...

  • 自定义小火箭

    ####自定义归属地的Toast 1. 在构造方法,构建Toast的params > 这段代码是从Toast的内部...

  • Android菜鸟起飞|Toast的高级使用

    Toast高级用法之设置带图片的Toast和自定义Toast 虽然google推出了更加美观实用的snackBar...

网友评论

      本文标题:自定义toast

      本文链接:https://www.haomeiwen.com/subject/dvadlftx.html