Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Über die Navigationsleiste können Nutzer zwischen Zielen in einer App wechseln. Sie sollten Navigationsleisten für Folgendes verwenden:
Drei bis fünf Ziele von gleicher Wichtigkeit
Kompakte Fenstergrößen
Einheitliche Ziele auf allen App-Bildschirmen
Abbildung 1: Eine Navigationsleiste mit vier Zielen.
Auf dieser Seite erfahren Sie, wie Sie in Ihrer App eine Navigationsleiste mit zugehörigen Bildschirmen und grundlegender Navigation anzeigen.
API-Oberfläche
Verwende die Composables NavigationBar und NavigationBarItem, um die Logik für das Wechseln des Ziels zu implementieren. Jedes NavigationBarItem steht für ein einzelnes Ziel.
NavigationBarItem umfasst die folgenden wichtigen Parameter:
selected: Gibt an, ob das aktuelle Element visuell hervorgehoben wird.
onClick(): Eine erforderliche Lambda-Funktion, die die Aktion definiert, die ausgeführt werden soll, wenn der Nutzer auf das Element klickt. Hier werden in der Regel Navigationsereignisse verarbeitet, der Status des ausgewählten Elements aktualisiert oder entsprechende Inhalte geladen.
label: Zeigt Text innerhalb des Elements an. Optional:
icon: Zeigt ein Symbol im Element an. Optional:
Beispiel: Navigationsleiste am unteren Rand
Im folgenden Snippet wird eine Navigationsleiste am unteren Bildschirmrand mit Elementen implementiert, damit Nutzer zwischen verschiedenen Bildschirmen in einer App wechseln können:
NavigationBar zeigt eine Sammlung von Elementen an, wobei jedes Element einem Destination entspricht.
val navController = rememberNavController() erstellt und speichert eine Instanz von NavHostController, die die Navigation in einem NavHost verwaltet.
var selectedDestination by rememberSaveable {
mutableIntStateOf(startDestination.ordinal) } verwaltet den Status des aktuell ausgewählten Elements.
var selectedDestination by rememberSaveable {
mutableIntStateOf(startDestination.ordinal) } verwaltet den Status des aktuell ausgewählten Elements.
startDestination.ordinal ruft den numerischen Index (die Position) des Destination.SONGS-Enum-Eintrags ab.
Wenn auf ein Element geklickt wird, wird navController.navigate(route = destination.route) aufgerufen, um zum entsprechenden Bildschirm zu wechseln.
Die onClick-Lambda-Funktion von NavigationBarItem aktualisiert den selectedDestination-Status, um das angeklickte Element visuell hervorzuheben.
Dazu wird die AppNavHost-Composable-Funktion aufgerufen und navController und startDestination übergeben, um den tatsächlichen Inhalt des ausgewählten Bildschirms anzuzeigen.
Ergebnis
Die folgende Abbildung zeigt die Navigationsleiste, die sich aus dem vorherigen Snippet ergibt:
Abbildung 2. Eine Navigationsleiste mit drei Zielen und den zugehörigen Symbolen: „Songs“, „Album“ und „Playlist“.
Alle Inhalte und Codebeispiele auf dieser Seite unterliegen den Lizenzen wie im Abschnitt Inhaltslizenz beschrieben. Java und OpenJDK sind Marken oder eingetragene Marken von Oracle und/oder seinen Tochtergesellschaften.
Zuletzt aktualisiert: 2025-08-27 (UTC).
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Benötigte Informationen nicht gefunden","missingTheInformationINeed","thumb-down"],["Zu umständlich/zu viele Schritte","tooComplicatedTooManySteps","thumb-down"],["Nicht mehr aktuell","outOfDate","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Problem mit Beispielen/Code","samplesCodeIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 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/5673ffc60b614daf028ee936227128eb8c4f9781/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)"]]