使用toaster开源库实现警告toast样式
首先在顶层build.gradle加入
buildscript { repositories { google() jcenter() } dependencies { classpath "com.android.tools.build:gradle:7.2.1" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }
在app的build.gradle加入依赖
implementation 'com.github.getActivity:Toaster:13.8'
在自定义Application类初始化
package com.xsh.rpgmvp2pngPro; import android.app.Application; import com.hjq.toast.Toaster; import com.simple.spiderman.SpiderMan; public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); SpiderMan.init(this); Toaster.init(this); } }
创建layout/toaster_warn.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/white" android:gravity="center_vertical" android:orientation="horizontal" android:paddingStart="20dp" android:paddingTop="12dp" android:paddingEnd="20dp" android:paddingBottom="12dp"> <TextView android:id="@android:id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:lineSpacingExtra="5dp" android:textColor="@android:color/holo_red_light" android:textSize="15sp"/> </LinearLayout>
封装好的函数
private void toasterWarn(String message) { ToastParams params = new ToastParams(); params.text = message; params.style = new CustomToastStyle(R.layout.toast_warn); Toaster.show(params); }
浙公网安备 33010602011771号