Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Nút hành động nổi (FAB) là một nút có độ nhấn mạnh cao, cho phép người dùng thực hiện một hành động chính trong ứng dụng. FAB thúc đẩy một hành động duy nhất, tập trung, đây là con đường phổ biến nhất mà người dùng có thể thực hiện và thường được cố định ở dưới cùng bên phải màn hình.
Hãy xem xét 3 trường hợp sử dụng sau đây mà bạn có thể dùng FAB:
Tạo mục mới: Trong một ứng dụng ghi chú, FAB có thể được dùng để nhanh chóng tạo một ghi chú mới.
Thêm người liên hệ mới: Trong một ứng dụng trò chuyện, FAB có thể mở một giao diện cho phép người dùng thêm người khác vào cuộc trò chuyện.
Vị trí trung tâm: Trong giao diện bản đồ, FAB có thể đặt vị trí hiện tại của người dùng làm tâm bản đồ.
Trong Material Design, có 4 loại FAB:
FAB: Nút hành động nổi có kích thước thông thường.
FAB nhỏ: Nút hành động nổi có kích thước nhỏ hơn.
FAB lớn: Nút hành động nổi có kích thước lớn hơn.
FAB mở rộng: Nút hành động nổi chứa nhiều nội dung hơn chỉ là một biểu tượng.
Hình 1. Bốn loại nút hành động nổi.
Nền tảng API
Mặc dù có một số thành phần kết hợp mà bạn có thể dùng để tạo các nút hành động nổi nhất quán với Material Design, nhưng các tham số của chúng không khác nhau nhiều.
Sau đây là một số tham số chính bạn nên lưu ý:
onClick: Hàm được gọi khi người dùng nhấn nút.
containerColor: Màu của nút.
contentColor: Màu của biểu tượng.
Nút hành động nổi
Để tạo một nút hành động nổi chung, hãy sử dụng thành phần kết hợp cơ bản FloatingActionButton. Ví dụ sau đây minh hoạ một cách triển khai cơ bản của FAB:
Để tạo một nút hành động nổi nhỏ, hãy dùng thành phần kết hợp SmallFloatingActionButton. Ví dụ sau đây minh hoạ cách thực hiện việc này, cùng với việc bổ sung màu tuỳ chỉnh.
Để tạo một nút hành động nổi lớn, hãy sử dụng thành phần kết hợp LargeFloatingActionButton. Thành phần kết hợp này không khác biệt đáng kể so với các ví dụ khác, ngoại trừ việc thành phần này tạo ra một nút lớn hơn.
Sau đây là một cách triển khai đơn giản cho FAB lớn.
Bạn có thể tạo các nút hành động nổi phức tạp hơn bằng thành phần kết hợp ExtendedFloatingActionButton. Điểm khác biệt chính giữa hàm này và FloatingActionButton là hàm này có các tham số icon và text riêng biệt. Các nút này cho phép bạn tạo một nút có nội dung phức tạp hơn và có thể điều chỉnh kích thước để phù hợp với nội dung của nút.
Đoạn mã sau đây minh hoạ cách triển khai ExtendedFloatingActionButton, với các giá trị mẫu được truyền cho icon và text.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-08-23 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-23 UTC."],[],[],null,["# Floating action button\n\nA Floating Action Button (FAB) is a high-emphasis button that lets the user\nperform a primary action in an application. It promotes a single, focused action\nthat is the most common pathway a user might take and is typically found\nanchored to the bottom right of the screen.\n\nConsider these three use cases where you might use a FAB:\n\n- **Create new item**: In a note-taking app, a FAB might be used to quickly create a new note.\n- **Add new contact**: In a chat app, a FAB could open an interface that lets the user add someone to a conversation.\n- **Center location**: In a map interface, a FAB could center the map on the user's current location.\n\nIn Material Design, there are four types of FAB:\n\n- **FAB**: A floating action button of ordinary size.\n- **Small FAB**: A smaller floating action button.\n- **Large FAB**: A larger floating action button.\n- **Extended FAB**: A floating action button that contains more than just an icon.\n\n**Figure 1.** The four floating action button types.\n\nAPI surface\n-----------\n\nAlthough there are several composables you can use to create floating action\nbuttons consistent with Material Design, their parameters don't differ greatly.\nAmong the key parameters you should keep in mind are the following:\n\n- `onClick`: The function called when the user presses the button.\n- `containerColor`: The color of the button.\n- `contentColor`: The color of the icon.\n\nFloating action button\n----------------------\n\nTo create a general floating action button, use the basic\n[`FloatingActionButton`](/reference/kotlin/androidx/compose/material3/package-summary#FloatingActionButton(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Shape,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.material3.FloatingActionButtonElevation,androidx.compose.foundation.interaction.MutableInteractionSource,kotlin.Function0)) composable. The following example demonstrates a\nbasic implementation of a FAB:\n\n\n```kotlin\n@Composable\nfun Example(onClick: () -\u003e Unit) {\n FloatingActionButton(\n onClick = { onClick() },\n ) {\n Icon(Icons.Filled.Add, \"Floating action button.\")\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/components/FloatingActionButton.kt#L61-L68\n```\n\n\u003cbr /\u003e\n\nThis implementation appears as follows:\n**Figure 2.** A floating action button.\n\nSmall button\n------------\n\nTo create a small floating action button, use the\n[`SmallFloatingActionButton`](/reference/kotlin/androidx/compose/material3/package-summary#SmallFloatingActionButton(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Shape,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.material3.FloatingActionButtonElevation,androidx.compose.foundation.interaction.MutableInteractionSource,kotlin.Function0)) composable. The following example demonstrates\nhow to do so, with the addition of custom colors.\n\n\n```kotlin\n@Composable\nfun SmallExample(onClick: () -\u003e Unit) {\n SmallFloatingActionButton(\n onClick = { onClick() },\n containerColor = MaterialTheme.colorScheme.secondaryContainer,\n contentColor = MaterialTheme.colorScheme.secondary\n ) {\n Icon(Icons.Filled.Add, \"Small floating action button.\")\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/components/FloatingActionButton.kt#L83-L92\n```\n\n\u003cbr /\u003e\n\n| **Note:** Because the various FAB composables share many parameters, you can use the approach in this example to customize colors with other composables.\n\nThis implementation appears as follows:\n**Figure 3.** A small floating action button.\n\nLarge button\n------------\n\nTo create a large floating action button, use the\n[`LargeFloatingActionButton`](/reference/kotlin/androidx/compose/material3/package-summary#LargeFloatingActionButton(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Shape,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.material3.FloatingActionButtonElevation,androidx.compose.foundation.interaction.MutableInteractionSource,kotlin.Function0)) composable. This composable is not\nsignificantly different from the other examples aside from the fact that it\nresults in a bigger button.\n\nThe following is a straightforward implementation of a large FAB.\n| **Note:** This example passes `CircleShape` as the value for the `shape` parameter, resulting in a round button, rather than a square with rounded borders. You can pass any instance of `Shape`, or set the value of `MaterialTheme.shape.large` to adjust it across your app.\n\n\n```kotlin\n@Composable\nfun LargeExample(onClick: () -\u003e Unit) {\n LargeFloatingActionButton(\n onClick = { onClick() },\n shape = CircleShape,\n ) {\n Icon(Icons.Filled.Add, \"Large floating action button\")\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/components/FloatingActionButton.kt#L96-L104\n```\n\n\u003cbr /\u003e\n\nThis implementation appears as follows:\n**Figure 4.** A large floating action button.\n\nExtended button\n---------------\n\nYou can create more complex floating action buttons with the\n[`ExtendedFloatingActionButton`](/reference/kotlin/androidx/compose/material3/package-summary#ExtendedFloatingActionButton(kotlin.Function0,kotlin.Function0,kotlin.Function0,androidx.compose.ui.Modifier,kotlin.Boolean,androidx.compose.ui.graphics.Shape,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.material3.FloatingActionButtonElevation,androidx.compose.foundation.interaction.MutableInteractionSource)) composable. The key difference between it\nand [`FloatingActionButton`](/reference/kotlin/androidx/compose/material3/package-summary#FloatingActionButton(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Shape,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.material3.FloatingActionButtonElevation,androidx.compose.foundation.interaction.MutableInteractionSource,kotlin.Function0)) is that it has dedicated `icon` and `text`\nparameters. They let you create a button with more complex content that scales\nto fit its content appropriately.\n\nThe following snippet demonstrates how to implement\n`ExtendedFloatingActionButton`, with example values passed for `icon` and\n`text`.\n\n\n```kotlin\n@Composable\nfun ExtendedExample(onClick: () -\u003e Unit) {\n ExtendedFloatingActionButton(\n onClick = { onClick() },\n icon = { Icon(Icons.Filled.Edit, \"Extended floating action button.\") },\n text = { Text(text = \"Extended FAB\") },\n )\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/components/FloatingActionButton.kt#L72-L79\n```\n\n\u003cbr /\u003e\n\nThis implementation appears as follows:\n**Figure 5.** A floating action button with both text and an icon.\n\nAdditional resources\n--------------------\n\n- [Common buttons](/develop/ui/compose/components/button)\n- [Material UI docs](https://m3.material.io/components/floating-action-button/overview)"]]