Android_自定义样式全局Toast

Android自带的toast样式不太好看,UI妹子可能会自己设计一个全局的提示,如图:

这时候就需要用到CustomView和toastUtil工具类了

一 : customView:

public class CustomToast extends Toast {

    TextView textView;
    /**
     * Construct an empty Toast object.  You must call {@link #setView} before you
     * can call {@link #show}.
     *
     * @param context The context to use.  Usually your {@link Application}
     *                or {@link Activity} object.
     */
    public CustomToast(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        View view = View.inflate(context, R.layout.layout_toast,null);
        textView = (TextView) view.findViewById(R.id.content);
        setView(view);
    }

    @Override
    public void setText(CharSequence sequence){
        textView.setText(sequence);
    }

    @Override
    public void setText(int resId) {
        textView.setText(resId);
    }

}

二 :ToastUtil

public class ToastUtil extends Toast{
    private static String oldMsg;
    protected static Toast toast = null;
    private static long oneTime = 0;
    private static long twoTime = 0;

    /**
     * Construct an empty Toast object.  You must call {@link #setView} before you
     * can call {@link #show}.
     *
     * @param context The context to use.  Usually your {@link Application}
     *                or {@link Activity} object.
     */
    public ToastUtil(Context context) {
        super(context);
    }

    public static void showToast(Context context, int resId) {
        showToast(context, context.getString(resId));
    }

    public static void showToast(Context context, int resId, int gravity) {
        showToast(context, context.getString(resId), gravity, 0, 0);
    }

    public static void showToast(Context context, String s, int gravity) {
        showToast(context, s, gravity, 0, 0);
    }

    public static void showToast(Context context, int resId, int gravity, int offX, int offY) {
        showToast(context, context.getString(resId), gravity, offX, offY);
    }

    public static void showToast(Context context, String s) {
        if (TextUtils.isEmpty(s)){
            return;
        }
        if (toast == null) {
            toast = new CustomToast(context);
            toast.setText(s);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER,0,0);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = s;
                toast.setText(s);
                toast.show();
            }
        }
        oneTime = twoTime;
    }


    public static void showToast(Context context, String s, int gravity, int offX, int offY) {
        if (toast == null) {
            toast = new CustomToast(context);
            toast.setText(s);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(gravity, offX, offY);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (s.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = s;
                toast.setText(s);
                toast.show();
            }
        }
        oneTime = twoTime;
    }
}
三:自定义样式Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_anjian_toast"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="30dp"
        android:paddingLeft="50dp"
        android:paddingRight="50dp"
        android:paddingTop="30dp"
        android:textColor="#fff"
        android:textSize="18dp" />

</LinearLayout>

Android 中,可以通过自定义 Toast 实现类似于悬浮通知的效果。具体步骤如下: 1. 创建一个自定义Toast 布局文件,例如 `custom_toast.xml`,可以在其中添加需要展示的内容,比如标题、内容、图标等。 2. 在代码中创建 Toast 对象,并将其设置为自定义的布局: ```java LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_container)); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); ``` 3. 如果需要实现悬浮通知的效果,可以将 Toast 的位置设置为 `Gravity.TOP | Gravity.LEFT`,并通过定时器不断更新 Toast 的位置。具体代码可以参考下面的示例: ```java final WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT); params.gravity = Gravity.TOP | Gravity.LEFT; params.x = 0; params.y = 100; final Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); final Handler handler = new Handler(); final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { handler.post(new Runnable() { @Override public void run() { params.y -= 10; toast.getView().setAlpha(toast.getView().getAlpha() - 0.1f); WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); windowManager.updateViewLayout(toast.getView(), params); if(params.y <= 0) { timer.cancel(); } } }); } }, 0, 100); ``` 注意:为了实现悬浮通知的效果,需要在 AndroidManifest.xml 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值