android 自定义吐司
Hello World, today we will be going to see how we can make custom toast and in this tutorial, we will see custom toast with an example. See below as an example.
您好,世界,今天我们将看到如何制作定制吐司,在本教程中,我们将通过示例查看定制吐司。 参见以下示例。
![Android Custom Toast gif](https://miro.medium.com/freeze/max/9999/0*q3SWg_ULe81NNdh6.gif)
Before starting let’s see.
开始之前,让我们看看。
什么是吐司? 什么时候使用? (What is Toast? When to use it?)
Toast is a view that contains text which notifies the user with some information.
Toast是一个包含文本的视图,该文本会向用户通知一些信息。
We can use toast when we want to notify the user.
当我们想通知用户时,可以使用烤面包。
用示例制作自定义吐司 (Making Custom Toast with Example)
To make custom toast we need to follow certain steps as follows.
要制作自定义吐司,我们需要遵循以下某些步骤。
步骤1.为Toast创建自定义布局 (Step 1. Create a Custom Layout for Toast)
the first step is to make a layout file for our toast.
第一步是为我们的吐司制作一个布局文件。
To make a layout file, right-click on layout folder under res and select new -> layout resource file.
要制作布局文件,请在res下的布局文件夹上单击鼠标右键,然后选择new-> layout resource file。
we will name this file as custom_toast.xml and in this file, we will make our layout which will b shown in the toast. Check the below code for the layout file.
我们将此文件命名为custom_toast.xml,在此文件中,我们将进行布局,该布局将在toast中显示 。 检查以下代码以获取布局文件。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/customToastContainer"
android:background="#E39D9D"
android:padding="10sp"
>
<ImageView
android:layout_width="50sp"
android:layout_height="50sp"
android:src="@drawable/ic_android"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:id="@+id/toastImage"
android:tint="@color/colorPrimaryDark"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toastText"
android:text="Custom Toast"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/toastImage"
app:layout_constraintBottom_toBottomOf="@id/toastImage"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
In the above code, we set the id to the parent view and we added an imageview and a textview.
在上面的代码中,我们将ID设置为父视图,并添加了imageview和textview。
We are keeping things simple so that you can understand better.
我们将事情保持简单,以便您可以更好地理解。
our layout looks like this below.
我们的布局如下所示。
![Image for post](https://miro.medium.com/max/9999/0*j6i7FVgUNZ_73gf7.png)
步骤2.将布局文件设置为Toast (Step 2. Set layout file to the Toast)
To trigger the toast we made a button in the activity layout and calling the makeToast function on click.
要触发吐司,我们在活动布局中创建了一个按钮,并在单击时调用makeToast函数。
Let’s see the code in the makeToast function.
让我们看看makeToast函数中的代码。
Hey if you want to read full article read here: Android Custom Toast With Example
嘿,如果您想阅读全文,请阅读: Android自定义吐司示例
翻译自: https://medium.com/swlh/android-custom-toast-with-example-44ce5fe6d6ec
android 自定义吐司