Gus*_*ria 3 android android-jetpack android-jetpack-compose android-compose-appbar
我想隐藏navigationIcon,但是当我通过null
或Unit
图标空间仍然可见时,如下图所示。
@Composable
fun DefaultScaffold(
title: String? = null,
icon: ImageVector? = null,
content: @Composable() () -> Unit
) {
Scaffold(
topBar = {
TopAppBar(
title = {
if (title != null) {
Text(text = "My Title")
}
},
navigationIcon = {
if (icon != null) {
IconButton(
onClick = {}
) {
Icon(imageVector = Icons.Filled.Close, contentDescription = null)
}
} else Unit
}
)
}
) {
content()
}
}
Run Code Online (Sandbox Code Playgroud)
删除条件语句周围的括号,然后在您不想显示图标空间时返回null
而不是返回。Unit
那么,就会变成
navigationIcon = if (icon != null) {
{
IconButton(
onClick = {}
) {
Icon(imageVector = Icons.Filled.Close, contentDescription = null)
}
}
} else null
Run Code Online (Sandbox Code Playgroud)
你可以使用类似的东西
navigationIcon = if (icon != null) {
{ /*... your code ..*/ }
} else null
Run Code Online (Sandbox Code Playgroud)
TopAppBar
但withtitle
和parameters的构造函数navigationIcon
保留了标题和导航图标的插槽。如果你检查源代码你可以发现:
if (navigationIcon == null) {
Spacer(TitleInsetWithoutIcon)
} else {
Run Code Online (Sandbox Code Playgroud)
您应该使用带有参数的构造函数content
,其内容没有限制。
然后你可以使用类似的东西:
TopAppBar(
content = {
Row(Modifier.fillMaxHeight(), verticalAlignment = Alignment.CenterVertically){
if (icon != null) {
IconButton(
onClick = {},
) {
Icon(imageVector = icon, contentDescription = null)
}
}
if (title != null) {
ProvideTextStyle(value = MaterialTheme.typography.h6) {
CompositionLocalProvider(
LocalContentAlpha provides ContentAlpha.high,
){
Text(text = title)
}
}
}
}
}
)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8493 次 |
最近记录: |