【问题标题】:What is the best way to create a custom toast from scratch?从头开始创建自定义吐司的最佳方法是什么?
【发布时间】:2020-05-03 03:56:34
【问题描述】:

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

我想知道自定义吐司的最佳方式是什么?创建片段?看法?还有其他想法吗?

感谢您的帮助。

【问题讨论】:

  • 你为什么不用library
  • @AndroidPlayer_Shree Toasty 库在里面使用 android Toast。

标签: android android-fragments android-activity android-view android-toast


【解决方案1】:

好的,您可以在此处创建自定义 toast,例如带有成功 toast 的绿色背景或带有错误 toast 的红色背景。只需按照说明一步一步地操作:

  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")

【讨论】:

  • 我想创建自己的Toast 类的实现,而不是使用android Toast
  • @wapn 然后你应该创建一个扩展 Toast 类的类,并通过覆盖 Toast 类的属性来自定义属性。
  • 不,我想彻底摆脱 android Toast 类。
  • @wapn 好的,然后您创建一个对话框片段并将其显示为不可取消,以便用户无法关闭它并在 onViewCreated() 函数中运行具有预期时间的 kotlin 协程运行块代码并调用dismiss( ) 延迟调用后的函数,它将关闭。这是没有自定义吐司的更新方式。到目前为止,我知道兄弟,你找不到其他有效的方法来做你想做的事。
  • 不需要额外的代码,这个库让你的工作更轻松github.com/GrenderG/Toasty
【解决方案2】:

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();

【讨论】:

  • 我不想自定义视图。我想创建Toast 类。
猜你喜欢
  • 2011-12-15
  • 2010-10-08
  • 1970-01-01
  • 2017-07-24
  • 2014-01-29
  • 2019-11-28
  • 1970-01-01
  • 2018-09-06
  • 1970-01-01
相关资源
最近更新 更多