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 cung cấp quyền truy cập vào các đích đến trong những ứng dụng chạy trên thiết bị có màn hình lớn. Bạn nên dùng thanh điều hướng cho:
Đích đến cấp cao nhất cần truy cập được ở mọi nơi trong ứng dụng
Từ 3 đến 7 điểm đến chính
Bố cục dành cho máy tính bảng hoặc máy tính
Hình 1. Thanh điều hướng có 4 đích đến và một nút hành động nổi.
Trang này hướng dẫn bạn cách hiển thị các thành phần dọc 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 thành phần kết hợp NavigationRail với NavigationRailItem để triển khai một thanh điều hướng dọc trong ứng dụng của bạn. NavigationRailItem biểu thị một mục riêng lẻ trong cột đường sắt.
NavigationRailItem bao gồm các thông số chính sau:
selected: Xác định xem mục trong danh sách phát 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 trên thanh điều hướng. Đâ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 trong thanh bên đã chọn hoặc tải nội dung tương ứng.
label: Hiển thị văn bản trong mục đường ray. Không bắt buộc.
icon: Hiển thị một biểu tượng trong mục thanh điều hướng. Không bắt buộc.
Ví dụ: Hệ thống điều hướng dựa trên thanh điều hướng dọc
Đoạn mã sau đây triển khai một dải điều hướng để người dùng có thể di chuyển giữa các màn hình khác nhau trong một ứng dụng:
NavigationRail hiển thị một cột dọc gồm các mục trên thanh điều hướng, 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 đường ray 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 người dùng nhấp vào một mục trên thanh điều hướng, 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 NavigationRailItem sẽ cập nhật trạng thái selectedDestination để làm nổi bật trực quan mục đường ray được nhấp vào.
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 kết quả của đoạn mã trước:
Hình 2. Một thanh điều hướng chứa 3 đích đến có cá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,["Rails provide access to destinations in apps that run on devices with large\nscreens. You should use navigation rails for:\n\n- Top-level destinations that need to be accessible anywhere in an app\n- Three to seven main destinations\n- Tablet or desktop layouts\n\n**Figure 1.** A navigation rail with four destinations and a floating action button.\n\nThis page shows you how to display rails in your app with related screens and\nbasic navigation.\n\nAPI surface\n\nUse the [`NavigationRail`](/reference/kotlin/androidx/compose/material3/package-summary#NavigationRail(androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,kotlin.Function1,androidx.compose.foundation.layout.WindowInsets,kotlin.Function1)) composable with [`NavigationRailItem`](/reference/kotlin/androidx/compose/material3/package-summary#NavigationRailItem(kotlin.Boolean,kotlin.Function0,kotlin.Function0,androidx.compose.ui.Modifier,kotlin.Boolean,kotlin.Function0,kotlin.Boolean,androidx.compose.material3.NavigationRailItemColors,androidx.compose.foundation.interaction.MutableInteractionSource)) to\nimplement a rail in your application. The `NavigationRailItem` represents a\nsingle rail item in the rail column.\n\n`NavigationRailItem` includes the following key parameters:\n\n- `selected`: Determines whether the current rail item is visually highlighted.\n- `onClick()`: A required lambda function that defines the action to be performed when the user clicks on the rail item. This is where you typically handle navigation events, update the selected rail item state, or load corresponding content.\n- `label`: Displays text within the rail item. Optional.\n- `icon`: Displays an icon within the rail item. Optional.\n\nExample: Rail-based navigation\n\nThe following snippet implements a navigation rail so users can navigate between\ndifferent 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 NavigationRailExample(modifier: Modifier = Modifier) {\n val navController = rememberNavController()\n val startDestination = Destination.SONGS\n var selectedDestination by rememberSaveable { mutableIntStateOf(startDestination.ordinal) }\n\n Scaffold(modifier = modifier) { contentPadding -\u003e\n NavigationRail(modifier = Modifier.padding(contentPadding)) {\n Destination.entries.forEachIndexed { index, destination -\u003e\n NavigationRailItem(\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 AppNavHost(navController, startDestination)\n }\n}https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/components/Navigation.kt#L153-L180\n```\n\n\u003cbr /\u003e\n\nKey points\n\n- `NavigationRail` displays a vertical column of rail 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 rail item.\n - `startDestination.ordinal` gets the numerical index (position) of the `Destination.SONGS` enum entry.\n- When a rail item is clicked, `navController.navigate(route =\n destination.route)` is called to navigate to the corresponding screen.\n- The `onClick` lambda of the `NavigationRailItem` updates the `selectedDestination` state to visually highlight the clicked rail 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 result of the previous snippet:\n**Figure 2.** A navigation rail that contains 3 destinations with associated icons: Songs, Album, and Playlist.\n\nAdditional resources\n\n- [Material 3 - Navigation rail](https://m3.material.io/components/navigation-rail/overview)\n- [Navigation with Compose](/develop/ui/compose/navigation)"]]