[null,null,["最后更新时间 (UTC):2025-07-26。"],[],[],null,["# Build and display a pop-up message\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add notifications in Compose. \n[Snackbar →](/develop/ui/compose/components/snackbar) \n\nYou can use a\n[Snackbar](/reference/com/google/android/material/snackbar/Snackbar) to\ndisplay a brief message to the user. Unlike\n[Notifications](/guide/topics/ui/notifiers/notifications), the\nmessage automatically goes away after a short period. A `Snackbar` is\nideal for brief messages that the user doesn't need to act on. For example, an\nemail app can use a `Snackbar` to tell the user that the app\nsuccessfully sent an email.\n\nUse a CoordinatorLayout\n-----------------------\n\nA `Snackbar` is attached to a view. The `Snackbar`\nprovides basic functionality if it is attached to any object derived from the\n[View](/reference/android/view/View) class, such as\nany of the common layout objects. However, if the `Snackbar` is\nattached to a\n[CoordinatorLayout](/reference/androidx/coordinatorlayout/widget/CoordinatorLayout),\nthe `Snackbar` gains additional features:\n\n- The user can dismiss the `Snackbar` by swiping it away.\n- The layout moves other UI elements when the `Snackbar` appears. For example, if the layout has a [FloatingActionButton](/reference/com/google/android/material/floatingactionbutton/FloatingActionButton), the layout moves the button up when it shows a `Snackbar`, instead of drawing the `Snackbar` on top of the button. You can see how this looks in figure 1.\n\nThe `CoordinatorLayout` class provides a superset of the\nfunctionality of\n[FrameLayout](/reference/android/widget/FrameLayout).\nIf your app already uses a `FrameLayout`, you can replace that layout\nwith a `CoordinatorLayout` to enable the full `Snackbar`\nfunctionality. If your app uses other layout objects, wrap your existing layout\nelements in a `CoordinatorLayout`, as shown in the following\nexample: \n\n```xml\n\u003candroid.support.design.widget.CoordinatorLayout\n android:id=\"@+id/myCoordinatorLayout\"\n xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\u003e\n\n \u003c!-- Here are the existing layout elements, now wrapped in\n a CoordinatorLayout. --\u003e\n \u003cLinearLayout\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:orientation=\"vertical\"\u003e\n\n \u003c!-- ...Toolbar, other layouts, other elements... --\u003e\n\n \u003c/LinearLayout\u003e\n\n\u003c/android.support.design.widget.CoordinatorLayout\u003e\n```\n\nSet an `android:id` tag for your `CoordinatorLayout`.\nYou need the layout's ID when you display the message.\n\n\n**Figure 1.** The `CoordinatorLayout` moves the\n`FloatingActionButton` up when the `Snackbar` appears.\n\nDisplay a message\n-----------------\n\nThere are two steps to displaying a message. First, you create a\n`Snackbar` object with the message text. Then, you call that object's\n[show()](/reference/com/google/android/material/snackbar/BaseTransientBottomBar#show())\nmethod to display the message to the user.\n\n### Create a Snackbar object\n\nCreate a `Snackbar` object by calling the static\n[Snackbar.make()](/reference/com/google/android/material/snackbar/Snackbar#make(android.view.View, int, int))\nmethod. When you create the `Snackbar`, specify the message it\ndisplays and the length of time to show the message: \n\n### Kotlin\n\n```kotlin\nval mySnackbar = Snackbar.make(view, stringId, duration)\n```\n\n### Java\n\n```java\nSnackbar mySnackbar = Snackbar.make(view, stringId, duration);\n```\n\n\nview\n:\n The view to attach the `Snackbar` to. The method searches up the\n view hierarchy from the passed view until it reaches a\n `CoordinatorLayout` or the window decor's content view.\n Ordinarily, it's simpler to pass the `CoordinatorLayout`\n enclosing your content.\n\n\nstringId\n:\n The resource ID of the message you want to display. This can be formatted or\n unformatted text.\n\n\nduration\n:\n The length of time to show the message. This can be\n [LENGTH_SHORT](/reference/com/google/android/material/snackbar/BaseTransientBottomBar#LENGTH_SHORT))\n or\n [LENGTH_LONG](/reference/com/google/android/material/snackbar/BaseTransientBottomBar#LENGTH_LONG).\n\n### Show the message to the user\n\nAfter you create the `Snackbar`, call its `show()`\nmethod to display the `Snackbar` to the user: \n\n### Kotlin\n\n```kotlin\nmySnackbar.show()\n```\n\n### Java\n\n```java\nmySnackbar.show();\n```\n\nThe system doesn't show multiple `Snackbar` objects at the same\ntime, so if the view is currently displaying another `Snackbar`, the\nsystem queues your `Snackbar` and displays it after the current\n`Snackbar` expires or is dismissed.\n\nIf you want to show a message to the user and don't need to call any of the\n`Snackbar` object's utility methods, you don't need to keep the\nreference to the `Snackbar` after you call `show()`. For\nthis reason, it's common to use method chaining to create and show a\n`Snackbar` in one statement: \n\n### Kotlin\n\n```kotlin\nSnackbar.make(\n findViewById(R.id.myCoordinatorLayout),\n R.string.email_sent,\n Snackbar.LENGTH_SHORT\n).show()\n```\n\n### Java\n\n```java\nSnackbar.make(findViewById(R.id.myCoordinatorLayout), R.string.email_sent,\n Snackbar.LENGTH_SHORT)\n .show();\n```"]]