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.
Thanh điều hướng cho phép người dùng chuyển đổi giữa các đích đến trong một ứng dụng. Bạn nên sử dụng thanh điều hướng cho:
Từ 3 đến 5 điểm đến có tầm quan trọng ngang nhau
Kích thước cửa sổ thu gọn
Các đích đến nhất quán trên các màn hình ứng dụng
Hình 1. Một thanh điều hướng có 4 đích đến.
Trang này hướng dẫn bạn cách hiển thị một thanh điều hướng trong ứng dụng của mình cùng với các màn hình liên quan và chế độ điều hướng cơ bản.
Nền tảng API
Sử dụng các thành phần kết hợp NavigationBar và NavigationBarItem để triển khai logic chuyển đổi đích đến. Mỗi NavigationBarItem đại diện cho một điểm đến duy nhất.
NavigationBarItem bao gồm các thông số chính sau:
selected: Xác định xem mục hiện tại có được làm nổi bật bằng hình ảnh hay không.
onClick(): Một hàm lambda bắt buộc xác định hành động sẽ được thực hiện khi người dùng nhấp vào mục. Đây là nơi bạn thường xử lý các sự kiện điều hướng, cập nhật trạng thái của mục đã chọn hoặc tải nội dung tương ứng.
label: Hiển thị văn bản trong mục. Không bắt buộc.
icon: Hiển thị một biểu tượng trong mục. Không bắt buộc.
Ví dụ: Thanh điều hướng dưới cùng
Đoạn mã sau đây triển khai một thanh điều hướng dưới cùng có các mục để người dùng có thể di chuyển giữa các màn hình trong một ứng dụng:
NavigationBar hiển thị một tập hợp các mục, trong đó mỗi mục tương ứng với một Destination.
val navController = rememberNavController() tạo và ghi nhớ một thực thể của NavHostController, đối tượng này quản lý hoạt động điều hướng trong một NavHost.
var selectedDestination by rememberSaveable {
mutableIntStateOf(startDestination.ordinal) } quản lý trạng thái của mục hiện được chọn.
var selectedDestination by rememberSaveable {
mutableIntStateOf(startDestination.ordinal) } quản lý trạng thái của mục hiện được chọn.
startDestination.ordinal lấy chỉ mục số (vị trí) của mục nhập enum Destination.SONGS.
Khi một mục được nhấp vào, navController.navigate(route = destination.route) sẽ được gọi để chuyển đến màn hình tương ứng.
Hàm lambda onClick của NavigationBarItem sẽ cập nhật trạng thái selectedDestination để làm nổi bật trực quan mục được nhấp.
Thao tác này gọi thành phần kết hợp AppNavHost, truyền navController và startDestination để hiển thị nội dung thực tế của màn hình đã chọn.
Kết quả
Hình ảnh sau đây cho thấy thanh điều hướng có được từ đoạn mã trước:
Hình 2. Một thanh điều hướng chứa 3 đích đến có biểu tượng liên kết: Bài hát, Đĩa nhạc và Danh sách phát.
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-27 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-27 UTC."],[],[],null,["The [navigation bar](https://m3.material.io/components/navigation-bar/overview) allows users to switch between destinations in an app. You\nshould use navigation bars for:\n\n- Three to five destinations of equal importance\n- Compact window sizes\n- Consistent destinations across app screens\n\n**Figure 1.** A navigation bar with 4 destinations.\n\nThis page shows you how to display a navigation bar in your app with related\nscreens and basic navigation.\n\nAPI surface\n\nUse the [`NavigationBar`](/reference/kotlin/androidx/compose/material3/package-summary#NavigationBar(androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.unit.Dp,androidx.compose.foundation.layout.WindowInsets,kotlin.Function1)) and [`NavigationBarItem`](/reference/kotlin/androidx/compose/material3/package-summary#(androidx.compose.foundation.layout.RowScope).NavigationBarItem(kotlin.Boolean,kotlin.Function0,kotlin.Function0,androidx.compose.ui.Modifier,kotlin.Boolean,kotlin.Function0,kotlin.Boolean,androidx.compose.material3.NavigationBarItemColors,androidx.compose.foundation.interaction.MutableInteractionSource)) composables to\nimplement destination switching logic. Each `NavigationBarItem` represents a\nsingular destination.\n\n`NavigationBarItem` includes the following key parameters:\n\n- `selected`: Determines whether the current item is visually highlighted.\n- `onClick()`: A required lambda function that defines the action to be performed when the user clicks on the item. This is where you typically handle navigation events, update the selected item state, or load corresponding content.\n- `label`: Displays text within the item. Optional.\n- `icon`: Displays an icon within the item. Optional.\n\nExample: Bottom navigation bar\n\nThe following snippet implements a bottom navigation bar with items so users can\nnavigate between different screens in an app:\n| **Note:** The [full source code](https://github.com/android/snippets/blob/main/compose/snippets/src/main/java/com/example/compose/snippets/components/Navigation.kt) includes the code that establishes the basic navigation structure for the following example.\n\n\n```kotlin\n@Composable\nfun NavigationBarExample(modifier: Modifier = Modifier) {\n val navController = rememberNavController()\n val startDestination = Destination.SONGS\n var selectedDestination by rememberSaveable { mutableIntStateOf(startDestination.ordinal) }\n\n Scaffold(\n modifier = modifier,\n bottomBar = {\n NavigationBar(windowInsets = NavigationBarDefaults.windowInsets) {\n Destination.entries.forEachIndexed { index, destination -\u003e\n NavigationBarItem(\n selected = selectedDestination == index,\n onClick = {\n navController.navigate(route = destination.route)\n selectedDestination = index\n },\n icon = {\n Icon(\n destination.icon,\n contentDescription = destination.contentDescription\n )\n },\n label = { Text(destination.label) }\n )\n }\n }\n }\n ) { contentPadding -\u003e\n AppNavHost(navController, startDestination, modifier = Modifier.padding(contentPadding))\n }\n}https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/components/Navigation.kt#L117-L148\n```\n\n\u003cbr /\u003e\n\nKey points\n\n- `NavigationBar` displays a collection of items, with each item corresponding to a `Destination`.\n- `val navController = rememberNavController()` creates and remembers an instance of [`NavHostController`](/reference/androidx/navigation/NavHostController), which manages the navigation within a [`NavHost`](/reference/androidx/navigation/NavHost).\n- `var selectedDestination by rememberSaveable {\n mutableIntStateOf(startDestination.ordinal)` } manages the state of the currently selected item.\n- `var selectedDestination by rememberSaveable {\n mutableIntStateOf(startDestination.ordinal) }` manages the state of the currently selected item.\n - `startDestination.ordinal` gets the numerical index (position) of the `Destination.SONGS` enum entry.\n- When an item is clicked, `navController.navigate(route = destination.route)` is called to navigate to the corresponding screen.\n- The `onClick` lambda of the `NavigationBarItem` updates the `selectedDestination` state to visually highlight the clicked item.\n- It calls the `AppNavHost` composable, passing the `navController` and `startDestination`, to display the actual content of the selected screen.\n\nResult\n\nThe following image shows the navigation bar resulting from the previous\nsnippet:\n**Figure 2.** A navigation bar that contains 3 destinations with associated icons: Songs, Album, and Playlist.\n\nAdditional resources\n\n- [Material 3 - Navigation Bar](https://m3.material.io/components/navigation-bar/overview)\n- [Navigation with Compose](/develop/ui/compose/navigation)"]]