Android上的自定义吐司:一个简单的例子

San*_*ndy 107 android toast

我是Android编程的新手.什么是在Android上显示自定义Toast通知的简单示例?

Dip*_*iya 187

使用以下自定义Toast的代码.它可能会帮助你.

toast.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:background="#DAAA" >

    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginRight="10dp" />

    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="#FFF" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
                               (ViewGroup) findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Run Code Online (Sandbox Code Playgroud)

并查看以下链接以获取自定义Toast.

带模拟时钟的自定义吐司

YouTube:在Android Studio中使用按钮创建自定义Toast

  • "(ViewGroup)findViewById(R.id.toast_layout_root)"可以替换为"null".因为你的活动不包含toast_layout所以它总是为null. (5认同)
  • 我的自定义吐司没有显示,因为我使用新的Constraint Layout作为我的自定义Toast的rootview.一旦我改为线性布局,一切都很完美.所以要警告...... (2认同)

The*_*uto 33

toast用于显示短时间间隔的消息; 因此,根据我的理解,您希望通过向其添加图像并更改消息文本的大小和颜色来自定义它.如果这就是你要做的全部,那么就没有必要进行单独的布局并将其扩展到Toast实例.

默认Toast的视图包含TextView用于在其上显示消息的视图.所以,如果我们有资源id引用TextView,我们可以使用它.以下是您可以做些什么来实现这一目标:

Toast toast = Toast.makeText(this, "I am custom Toast!", Toast.LENGTH_LONG);
View toastView = toast.getView(); // This'll return the default View of the Toast.

/* And now you can get the TextView of the default View of the Toast. */
TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message);
toastMessage.setTextSize(25);
toastMessage.setTextColor(Color.RED);
toastMessage.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_fly, 0, 0, 0);
toastMessage.setGravity(Gravity.CENTER);
toastMessage.setCompoundDrawablePadding(16);
toastView.setBackgroundColor(Color.CYAN);
toast.show();
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,您可以看到,您可以通过setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)相对于TextView的任何位置将图像添加到TextView .

输出:

在此输入图像描述


ρяσ*_*я K 15

步骤1:

首先在以下位置创建自定义Toast的布局res/layout/custom_toast.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_layout_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFF"
    android:orientation="horizontal"
    android:padding="5dp" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#000" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

第2步:在活动代码中,获取上面的自定义视图并附加到Toast:

// Get your custom_toast.xml ayout
LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout_id));

// set a message
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Button is clicked!");

// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Run Code Online (Sandbox Code Playgroud)

有关更多帮助,请参阅我们如何在Android中创建自定义Toast:

http://developer.android.com/guide/topics/ui/notifiers/toasts.html

  • 如果这有用那么应该接受他的答案. (4认同)

Dee*_*ami 6

请参阅此处的链接。你找到你的解决方案。并尝试:

创建自定义 Toast 视图

如果简单的文本消息还不够,您可以为 Toast 通知创建自定义布局。要创建自定义布局,请在 XML 或应用程序代码中定义一个 View 布局,并将根 View 对象传递给 setView (View) 方法。

例如,您可以使用以下 XML(另存为 toast_layout.xml)为右侧屏幕截图中可见的 toast 创建布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toast_layout_root"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="10dp"
            android:background="#DAAA"
>

    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
    />

    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
    />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

请注意,LinearLayout 元素的 ID 是“toast_layout”。您必须使用此 ID 从 XML 扩展布局,如下所示:

 LayoutInflater inflater = getLayoutInflater();
 View layout = inflater.inflate(R.layout.toast_layout,
                                (ViewGroup) findViewById(R.id.toast_layout_root));

 ImageView image = (ImageView) layout.findViewById(R.id.image);
 image.setImageResource(R.drawable.android);
 TextView text = (TextView) layout.findViewById(R.id.text);
 text.setText("Hello! This is a custom toast!");

 Toast toast = new Toast(getApplicationContext());
 toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
 toast.setDuration(Toast.LENGTH_LONG);
 toast.setView(layout);
 toast.show();
Run Code Online (Sandbox Code Playgroud)

首先,使用 getLayoutInflater()(或 getSystemService())检索 LayoutInflater,然后使用 inflate(int, ViewGroup) 从 XML 扩展布局。第一个参数是布局资源 ID,第二个参数是根视图。你可以使用这个膨胀的布局在布局中找到更多的 View 对象,所以现在捕获和定义 ImageView 和 TextView 元素的内容。最后,使用 Toast(Context) 创建一个新的 Toast 并设置 toast 的一些属性,例如重力和持续时间。然后调用 setView(View) 并将膨胀的布局传递给它。您现在可以通过调用 show() 使用自定义布局显示 Toast。

注意:不要为 Toast 使用公共构造函数,除非您打算使用 setView(View) 定义布局。如果您没有要使用的自定义布局,则必须使用 makeText(Context, int, int) 来创建 Toast。


Ano*_*p M 6

注意,Android 11 中的 toast 更新

来自后台的自定义 Toast 会被阻止,Android 11 通过弃用自定义 Toast 视图来保护用户。出于安全原因并为了保持良好的用户体验,如果包含自定义视图的 Toast 是由面向 Android 11 的应用从后台发送的,系统会阻止这些 Toast。

Android R 中添加的addCallback()方法 如果您希望在 Toast(文本或自定义)出现或消失时收到通知。

Toast API中最重要的文本更改为,对于面向 Android 11 的应用程序,getView()方法在访问它时返回 null,因此,请确保保护您的应用程序免受致命异常的影响,您知道我的意思:)

如果适用,请使用小吃店。

建议您尽可能使用小吃店。如果您的应用程序的用例阻止您使用小吃栏,例如当您的应用程序在后台时需要向用户发送消息时,您仍然可以使用文本 toast,因为它们不受新行为更改的限制。

有关该主题的更多详细信息,请参阅官方文档。