提示
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
试用 Compose 方式
Jetpack Compose 是推荐在 Android 设备上使用的界面工具包。了解如何在 Compose 中添加组件。
<ph type="x-smartling-placeholder">
</ph>
弹出式窗口 →
提示是在用户长按视图或将鼠标悬停在视图上时,显示在视图旁边的简短描述性消息。如果您的应用使用图标表示某项操作或某条信息以节省布局中的空间,使用提示会非常有用。本页介绍了如何在 Android 8.0(API 级别 26)及更高版本中添加这些提示。
在某些情况下(例如在办公应用中),需要采用描述性方法来传达具体提示和操作。您可以使用提示来显示一条描述性消息,如图 1 中所示。

图 1. Android 应用中显示的提示。
某些标准的微件会根据 title
或 content description
属性的内容显示提示。从 Android 8.0 开始,您可以指定提示中显示的文本,无需考虑其他属性的值。
设置提示文本
您可以通过调用 setTooltipText()
方法在 View
中指定提示文本。您可以使用相应的 XML 属性或 API 来设置 tooltipText
属性。
如需在 XML 文件中指定提示文本,请设置 android:tooltipText
属性,如下例所示:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:tooltipText="Send an email" />
如需在代码中指定提示文本,请使用 setTooltipText(CharSequence)
方法,如下例所示:
Kotlin
val fab: FloatingActionButton = findViewById(R.id.fab)
fab.tooltipText = "Send an email"
Java
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setTooltipText("Send an email");
API 还包含一个 getTooltipText()
方法,可用于检索 tooltipText
属性的值。
当用户将鼠标悬停在视图上或长按视图时,Android 会显示 tooltipText
属性的值。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Tooltips\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add components in Compose. \n[Popup →](/reference/kotlin/androidx/compose/ui/window/package-summary#Popup(androidx.compose.ui.window.PopupPositionProvider,kotlin.Function0,androidx.compose.ui.window.PopupProperties,kotlin.Function0)) \n\n\u003cbr /\u003e\n\nA tooltip is a small descriptive message that appears near a view when users\nlong press the view or hover their mouse over it. This is useful when your app\nuses an icon to represent an action or piece of information to save space in the\nlayout. This page shows you how to add these tooltips on Android 8.0 (API level\n26) and higher.\n\nSome scenarios, such as those in productivity apps, require a descriptive method\nof communicating ideas and actions. You can use tooltips to display a\ndescriptive message, as shown in figure 1.\n\n**Figure 1.** Tooltip displayed in an Android app.\n\nSome standard widgets display tooltips based on the content of the `title` or\n`content description` properties. Starting in Android 8.0, you can specify the\ntext displayed in the tooltip regardless of the value of other properties.\n\nSetting the tooltip text\n------------------------\n\nYou can specify the tooltip text in a [View](/reference/android/view/View) by calling the\n[setTooltipText()](/reference/android/view/View#setTooltipText(java.lang.CharSequence)) method. You can set\nthe `tooltipText` property using the corresponding XML attribute or API.\n\nTo specify the tooltip text in your XML files, set the [android:tooltipText](/reference/android/R.styleable#View_tooltipText) attribute, as shown\nin the following example: \n\n \u003candroid.support.design.widget.FloatingActionButton\n android:id=\"@+id/fab\"\n android:tooltipText=\"Send an email\" /\u003e\n\nTo specify the tooltip text in your code, use the [setTooltipText(CharSequence)](/reference/android/view/View#setTooltipText(java.lang.CharSequence)) method, as shown in the following example: \n\n### Kotlin\n\n```kotlin\nval fab: FloatingActionButton = findViewById(R.id.fab)\nfab.tooltipText = \"Send an email\"\n```\n\n### Java\n\n```java\nFloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);\nfab.setTooltipText(\"Send an email\");\n```\n\nThe API also includes a [getTooltipText()](/reference/android/view/View#getTooltipText()) method that\nyou can use to retrieve the value of the `tooltipText` property.\n\nAndroid displays the value of the `tooltipText` property when users hover their\nmouse over the view or long press the view."]]