我正在try 将edge to edge效果应用于Compose Material 3,但system navigation bar在某些设备上变得更暗.
可组合主题:
@Composable
fun AppTheme(
systemUiController: SystemUiController = rememberSystemUiController(),
useDarkColors: Boolean = isSystemInDarkTheme(),
enableDynamicColors: Boolean = true,
content: @Composable () -> Unit
) {
val colorScheme = when {
enableDynamicColors && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ->
if (useDarkColors) dynamicDarkColorScheme(LocalContext.current)
else dynamicLightColorScheme(LocalContext.current)
useDarkColors -> darkColorScheme()
else -> lightColorScheme()
}
DisposableEffect(key1 = Unit) {
systemUiController.setSystemBarsColor(
color = Color.Transparent,
darkIcons = colorScheme.background.luminance() > 0.5
)
onDispose { }
}
MaterialTheme(
colorScheme = colorScheme,
typography = Typography(),
shapes = Shapes(),
content = content
)
}
在上面的代码中,我将条定义为透明,并且只有当背景 colored颜色 亮度高于特定值时,图标才应该是暗的.
OBS:SystemUiController
来自Accompanist System UI Controller个图书馆.
在hemes.xml中:
<style name="AppTheme" parent="android:Theme.Material.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
</style>
主要可合成内容:
@OptIn(ExperimentalMaterial3Api::class)
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
setContent {
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
AppTheme {
Scaffold(
modifier = Modifier
.fillMaxSize()
.nestedScroll(connection = scrollBehavior.nestedScrollConnection),
topBar = {
CenterAlignedTopAppBar(
modifier = Modifier.fillMaxWidth(),
title = { Text(text = "App Bar") },
scrollBehavior = scrollBehavior
)
},
bottomBar = {
NavigationBar(modifier = Modifier.fillMaxWidth()) {
repeat(times = 3) { index ->
NavigationBarItem(
selected = index == 0,
onClick = { },
icon = {
Icon(
imageVector = Icons.Default.Home,
contentDescription = null
)
},
label = { Text(text = "Item $index") }
)
}
}
},
contentWindowInsets = WindowInsets.systemBars
) { contentPadding ->
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues = contentPadding),
contentPadding = PaddingValues(all = 16.dp),
verticalArrangement = Arrangement.spacedBy(space = 16.dp)
) {
items(count = 50) { index ->
Text(text = "Item $index")
}
}
}
}
}
}
}
这里有一个简单的例子,说明system navigation bar变得更暗了.
system navigation bar是透明的,但由于某种原因,它应用了更暗的效果,因为NavigationBar
.如果我们go 掉NavigationBar
,我们将看到它通常遵循背景 colored颜色 ,无论它是带有动态 colored颜色 的浅色还是深色主题.
正如我在一开始所 comments 的,这个"问题"并不是在所有的设备上都会发生,在我的Android 12物理设备上,这个问题只会在轻量级主题时发生,在Android 13的模拟器上,无论主题是什么,问题都会发生.
例如,与Gmail app相比,系统导航栏保持与NavigationBar
相同的 colored颜色 ,这是我想要的效果,而不是应用更暗的效果……
Problem when using NavigationBar
: