CenterAlignedTopAppBar
Common
Component in Material 3 Compose
Top app bars display information and actions at the top of a screen.
This small top app bar has a header title that is horizontally aligned to the center.
Last updated:
Installation
dependencies {
implementation("androidx.compose.material3:material3:1.4.0-alpha02")
}
Overloads
@Deprecated(
message = "Deprecated in favor of CenterAlignedTopAppBar with expandedHeight parameter",
level = DeprecationLevel.HIDDEN
)
@ExperimentalMaterial3Api
@Composable
fun CenterAlignedTopAppBar(
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
navigationIcon: @Composable () -> Unit = {},
actions: @Composable RowScope.() -> Unit = {},
windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
colors: TopAppBarColors = TopAppBarDefaults.centerAlignedTopAppBarColors(),
scrollBehavior: TopAppBarScrollBehavior? = null
)
Parameters
name | description |
---|---|
title | the title to be displayed in the top app bar |
modifier | the [Modifier] to be applied to this top app bar |
navigationIcon | the navigation icon displayed at the start of the top app bar. This should typically be an [IconButton] or [IconToggleButton]. |
actions | the actions displayed at the end of the top app bar. This should typically be [IconButton]s. The default layout here is a [Row], so icons inside will be placed horizontally. |
windowInsets | a window insets that app bar will respect. |
colors | [TopAppBarColors] that will be used to resolve the colors used for this top app bar in different states. See [TopAppBarDefaults.centerAlignedTopAppBarColors]. |
scrollBehavior | a [TopAppBarScrollBehavior] which holds various offset values that will be applied by this top app bar to set up its height and colors. A scroll behavior is designed to work in conjunction with a scrolled content to change the top app bar appearance as the content scrolls. See [TopAppBarScrollBehavior.nestedScrollConnection]. |
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@ExperimentalMaterial3Api
@Composable
fun CenterAlignedTopAppBar(
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
navigationIcon: @Composable () -> Unit = {},
actions: @Composable RowScope.() -> Unit = {},
expandedHeight: Dp = TopAppBarDefaults.TopAppBarExpandedHeight,
windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
colors: TopAppBarColors = TopAppBarDefaults.centerAlignedTopAppBarColors(),
scrollBehavior: TopAppBarScrollBehavior? = null
)
Parameters
name | description |
---|---|
title | the title to be displayed in the top app bar |
modifier | the [Modifier] to be applied to this top app bar |
navigationIcon | the navigation icon displayed at the start of the top app bar. This should typically be an [IconButton] or [IconToggleButton]. |
actions | the actions displayed at the end of the top app bar. This should typically be [IconButton]s. The default layout here is a [Row], so icons inside will be placed horizontally. |
expandedHeight | this app bar's height. When a specified [scrollBehavior] causes the app bar to collapse or expand, this value will represent the maximum height that the bar will be allowed to expand. This value must be specified and finite, otherwise it will be ignored and replaced with [TopAppBarDefaults.TopAppBarExpandedHeight]. |
windowInsets | a window insets that app bar will respect. |
colors | [TopAppBarColors] that will be used to resolve the colors used for this top app bar in different states. See [TopAppBarDefaults.centerAlignedTopAppBarColors]. |
scrollBehavior | a [TopAppBarScrollBehavior] which holds various offset values that will be applied by this top app bar to set up its height and colors. A scroll behavior is designed to work in conjunction with a scrolled content to change the top app bar appearance as the content scrolls. See [TopAppBarScrollBehavior.nestedScrollConnection]. |
Code Example
SimpleCenterAlignedTopAppBar
/**
* A sample for a simple use of [CenterAlignedTopAppBar].
*
* The top app bar here does not react to any scroll events in the content under it.
*/
@OptIn(ExperimentalMaterial3Api::class)
@Preview
@Composable
fun SimpleCenterAlignedTopAppBar() {
Scaffold(
topBar = {
CenterAlignedTopAppBar(
title = {
Text("Centered TopAppBar", maxLines = 1, overflow = TextOverflow.Ellipsis)
},
navigationIcon = {
IconButton(onClick = { /* doSomething() */ }) {
Icon(
imageVector = Icons.Filled.Menu,
contentDescription = "Localized description"
)
}
},
actions = {
IconButton(onClick = { /* doSomething() */ }) {
Icon(
imageVector = Icons.Filled.Favorite,
contentDescription = "Localized description"
)
}
}
)
},
content = { innerPadding ->
LazyColumn(
contentPadding = innerPadding,
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
val list = (0..75).map { it.toString() }
items(count = list.size) {
Text(
text = list[it],
style = MaterialTheme.typography.bodyLarge,
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp)
)
}
}
}
)
}