使用键盘 IME 动画

您可以将 Modifier.imeNestedScroll() 应用于滚动容器,以便在滚动到容器底部时自动打开和关闭 IME。

class WindowInsetsExampleActivity : AppCompatActivity() {  override fun onCreate(savedInstanceState: Bundle?) {  super.onCreate(savedInstanceState)  WindowCompat.setDecorFitsSystemWindows(window, false)  setContent {  MaterialTheme {  MyScreen()  }  }  } } @OptIn(ExperimentalLayoutApi::class) @Composable fun MyScreen() {  Box {  LazyColumn(  modifier = Modifier  .fillMaxSize() // fill the entire window  .imePadding() // padding for the bottom for the IME  .imeNestedScroll(), // scroll IME at the bottom  content = { }  )  FloatingActionButton(  modifier = Modifier  .align(Alignment.BottomEnd)  .padding(16.dp) // normal 16dp of padding for FABs  .navigationBarsPadding() // padding for navigation bar  .imePadding(), // padding for when IME appears  onClick = { }  ) {  Icon(imageVector = Icons.Filled.Add, contentDescription = "Add")  }  } }

展示界面元素上下滚动以便为键盘腾出空间的动画
图 3. IME 动画。