从头开始创建自定义吐司的最佳方法是什么?

问题描述 投票:0回答:2

我想从头开始制作自己的吐司。 这意味着我在任何方面都不想使用android Toast类。我的祝酒将在MainActivity中实现,并由EventBus从应用程序中的任何位置调用。在我的应用程序中,我只有一个活动(MainActivity)和许多片段作为屏幕。在我的吐司中,我想实现与android Toast中类似的功能(例如,在3秒后隐藏,在同时出现多个吐司时创建队列等),但是我不想使用android Toast类。

我想知道做定制吐司的最佳方法是什么?创建片段?视图?还有其他想法吗?

感谢您的帮助。

android android-fragments android-activity android-view android-toast
2个回答
0
投票

[确定,您可以创建自定义吐司,例如带有成功吐司的绿色背景或带有错误吐司的红色背景。只需按照说明进行操作即可:

  1. 首先将给定的两个功能复制并粘贴到类声明之外的kotlin文件中,以便您可以从项目中的任何位置进行访问。

    fun showErrorToast(context: Context, message: String) {
        val parent: ViewGroup? = null
        val toast = Toast.makeText(context, "", Toast.LENGTH_LONG)
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val toastView = inflater.inflate(R.layout.toast_custom_error, parent)
        toastView.errorMessage.text = message
        toast.view = toastView
        toast.show()
    }
    
    fun showSuccessToast(context: Context, message: String) {
        val parent: ViewGroup? = null
        val toast = Toast.makeText(context, "", Toast.LENGTH_LONG)
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val toastView = inflater.inflate(R.layout.toast_custom_success, parent)
        toastView.successMessage.text = message
        toast.view = toastView
        toast.show()
    }
    
  2. 然后创建两个名为“ toast_custom_error”和“ toast_custom_success”的xml布局文件,然后将这两个xml布局代码复制并粘贴到这些文件中。

    “ toast_custom_error”

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/container"
            android:orientation="vertical"
            android:background="@drawable/rounded_bg_error"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <TextView
                android:id="@+id/errorMessage"
                android:textColor="#FFFFFF"
                android:textSize="16sp"
                android:gravity="center"
                android:layout_marginStart="12dp"
                android:layout_marginEnd="12dp"
                android:layout_marginTop="12dp"
                android:layout_marginBottom="12dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

    “ toast_custom_success”

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/container"
            android:orientation="vertical"
            android:background="@drawable/rounded_bg_success"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <TextView
                android:id="@+id/successMessage"
                android:textColor="#FFFFFF"
                android:textSize="16sp"
                android:gravity="center"
                android:layout_marginStart="12dp"
                android:layout_marginEnd="12dp"
                android:layout_marginTop="12dp"
                android:layout_marginBottom="12dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
    </LinearLayout>
    
  3. 然后创建两个名为“ rounded_bg_error”和“ rounded_bg_success”的可绘制xml文件,并将以下代码粘贴到其中:

    “ rounded_bg_error”

    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <corners android:radius="5dp"/>
        <solid android:color="#F81616" />
    </shape>
    

    “ rounded_bg_success”

    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <corners android:radius="5dp"/>
        <solid android:color="#B2FF59" />
    </shape>
    
  4. 最后根据需要从项目的任何位置调用上述两个函数:

从片段->

    showErrorToast(requireContext(), "Your Error Message")
    showSuccessToast(requireContext(), "Your Success Message")

从活动->

    showErrorToast(this, "Your Error Message")
    showSuccessToast(this, "Your Success Message")

0
投票

customt_toast.xml

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootId"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView android:id="@+id/textView"
       android:layout_width="wrap_content"
       android:layout_height="match_parent"/>

</FrameLayout>

您认为:

View toastLayout = getLayoutInflater().inflate(R.layout.custom_toast, 
                   (R.id.rootId));

TextView textView = (TextView) layout.findViewById(R.id.textView);
textView.setText("blah blah");

Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(toastLayout);
toast.show();
© www.soinside.com 2019 - 2024. All rights reserved.