Android-自定义Toast

197 阅读1分钟

方法 CustomToast.java

public class CustomToast {
    private static Toast mToast;
    private static Handler mHandler;
    private static Runnable mToastRunnable;

    public static void showToast(final Activity activity, final String text, final int duration) {
        if (mToast == null) {
            mHandler = new Handler(Looper.getMainLooper());
            mToastRunnable = new Runnable() {
                @Override
                public void run() {
                    if (activity == null || activity.isFinishing()) {
                        return;
                    }
                    View toastLayout = LayoutInflater.from(activity).inflate(R.layout.custom_toast, null);
                    ImageView imageView = toastLayout.findViewById(R.id.imageView);
                    TextView textView = toastLayout.findViewById(R.id.textView);
                    textView.setText(text);
                    imageView.setImageResource(R.drawable.ic_toast); // Set your custom image resource here  
                    Toast toast = new Toast(activity);
                    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); // Set the position of the toast  
                    toast.setDuration(duration);
                    toast.setView(toastLayout);
                    toast.show();
                }
            };
        } else {
            mHandler.removeCallbacks(mToastRunnable);
        }
        mHandler.postDelayed(mToastRunnable, duration);
    }
}

//xml custom_toast.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:orientation="vertical"
   android:padding="44dp"
   android:background="@drawable/custom_toast_background">
   <TextView
       android:id="@+id/textView"
       android:text="1"
       android:textColor="@color/white"
       android:textSize="17dp"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
</LinearLayout>

// custom_toast_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <corners android:radius="20dp" />
 <solid android:color="@color/primary_color" />
</shape>

//全屏显示dialog

alertDialog.show();
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(alertDialog.getWindow().getAttributes());
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; // 设置宽度
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; // 设置高度
alertDialog.getWindow().setAttributes(layoutParams);

<style name="DebugDialog" parent="@android:style/Theme.Material.Dialog">
    <!-- 是否有边框 -->
    <item name="android:windowFrame">@null</item>
    <!--是否在悬浮Activity之上  -->
    <item name="android:windowIsFloating">true</item>
    <!-- 标题 -->
    <item name="android:windowNoTitle">true</item>
    <!--阴影  -->
    <item name="android:windowIsTranslucent">true</item><!--半透明-->
    <!--背景透明-->
    <item name="android:windowBackground">@android:color/transparent</item>
    <!-- 还可以加入一些弹出和退出的动画 (lan)-->
</style>