[英]Why does accompanist-systemuicontroller not work when using Jetpack Compose with Material3?
I'm learning the use of Material3 in jetpack compose, and I'm trying to set the statusbar to be transparent just as I used to.我正在学习在 Jetpack Compose 中使用 Material3,并且我正在尝试将状态栏设置为透明,就像我以前一样。 However, with the following code:
但是,使用以下代码:
WindowCompat.setDecorFitsSystemWindows(window, false)
setContent {
val systemUiController = rememberSystemUiController()
SideEffect {
systemUiController.setSystemBarsColor(
color = Color.Transparent,
darkIcons = true
)
}
TestStatusBarTheme {
Surface(
modifier = Modifier
.statusBarsPadding()
.fillMaxSize(),
color = MaterialTheme.colorscheme.background
) {
Text(text = "Test")
}
}
}
While navigationbar becomes transparent, statusbar does not change anyway.虽然导航栏变得透明,但状态栏无论如何都不会改变。 Then I apply the same code, this time using original material design library while keeping everything else unchanged, and it works properly, as the statusbar turns into transparent too.
然后我应用相同的代码,这次使用原始材料设计库,同时保持其他一切不变,它工作正常,因为状态栏也变成透明的。
I can't figure out why I can't use accompanist to change the statusbar in material3.我想不通为什么我不能使用伴奏来更改material3中的状态栏。 As navigationbar becomes transparent, it's obvious that the systemUiController has got the window and can make changes to navigationbar, then why it can't work with statusbar, which also is a systembar?
既然navigationbar变透明了,很明显systemUiController已经拿到了window,可以对navigationbar进行修改,那么为什么statusbar也是一个systembar就不行了呢? Is there anything new I haven't notice to make accompanist-systemuicontroller cooperate with Material3, or is it just an unfixed bug for the current version of Material3 or accompanist?
accompanist-systemuicontroller 配合Material3 是不是有什么我没注意到的新东西,还是只是Material3 或accompanist 当前版本的一个未修复的bug?
My compose version is 1.2.0-beta02, accompanist version is 0.24.9-beta, and material3 version is 1.0.0-alpha12.我的compose版本是1.2.0-beta02,伴奏版本是0.24.9-beta,material3版本是1.0.0-alpha12。
I was trying to move to Windows.Insets API in 1.2.0-rc02 version of compose.我试图在 1.2.0-rc02 版本的 compose 中迁移到 Windows.Insets API。 And looks like it's working.
看起来它正在工作。 Try to start my sample:
尝试开始我的示例:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
makeStatusBarTransparent()
//WindowCompat.setDecorFitsSystemWindows(window, false)
setContent {
Box(
Modifier
.background(Color.Blue)
.fillMaxSize()
.padding(top = 10.dp, bottom = 10.dp)
.statusBarsPadding() //systemBarsPadding
) {
//Box(Modifier.background(Color.Green).navigationBarsPadding()) {
Greeting("TopStart", Alignment.TopStart)
Greeting("BottomStart", Alignment.BottomStart)
Greeting("TopEnd", Alignment.TopEnd)
Greeting("BottomEnd", Alignment.BottomEnd)
//}
}
}
/* setContent {
MyComposeApp1Theme {
// A surface container using the 'background' color from the theme
Surface(modifier = Modifier.fillMaxSize(), color = Color.Red) {
Box(Modifier
.fillMaxSize()
.padding(top = 34.dp)
) {
Greeting("Android")
}
}
}
}*/
}
}
@Composable
fun Greeting(name: String, contentAlignment: Alignment) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = contentAlignment
) {
Text(
text = "Hello $name!",
Modifier
.background(color = Color.Cyan)
)
}
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
MyComposeApp1Theme {
Greeting("Android", Alignment.TopStart)
}
}
@Suppress("DEPRECATION")
fun Activity.makeStatusBarTransparent() {
window.apply {
clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
statusBarColor = android.graphics.Color.GREEN//android.graphics.Color.TRANSPARENT
}
}
val Int.dp
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
toFloat(),
Resources.getSystem().displayMetrics
)
It looks like Material3 version of Scaffold
is adding insets by default:看起来 Material3 版本的
Scaffold
默认添加了 insets:
@ExperimentalMaterial3Api
@Composable
fun Scaffold(
...
contentWindowInsets: WindowInsets = ScaffoldDefaults.contentWindowInsets,
content: @Composable (PaddingValues) -> Unit
) {
So if you want to add status bar padding, you have to remove top inset:所以如果你想添加状态栏填充,你必须删除顶部插图:
Scaffold(
modifier = Modifier
.statusBarsPadding(),
contentWindowInsets = WindowInsets(top = 0.dp)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.