android ModalBottomSheet显示在Jetpack Compose中的BottomBar上方

i7uq4tfw  于 2023-06-27  发布在  Android
关注(0)|答案(2)|浏览(228)

我在ComposeActivity中添加了BottomBarBottomBar负责显示3个可组合。我不想添加ModalBottomSheetActivity,而是在第三屏幕组合中添加了ModalBottomSheet。当调用ModalBottomSheet时,它会显示在BottomBar的上方。如何ModalBottomSheet可以显示时,呼吁行动是从第三屏幕组合,它应该高于BottomBar。任何帮助将不胜感激。

它应显示在BottomBar上方和底部。

j2qf4p5b

j2qf4p5b1#

只需将ModalBottomSheetLayout定位在脚手架组件的顶部。
示例:

  1. AppTheme() {
  2. ModalBottomSheetLayout(){ //<-- Here
  3. Scaffold(){
  4. //<-- Not Here or below
  5. NavigationGraph(){
  6. }
  7. }
  8. }
  9. }

然后从NavHost调用bottomSheet。
你也可以隐藏底部栏与行动从屏幕上,但它的长路;

  • 它可能因组件子结构而异

屏幕:

  1. @Composable
  2. fun 3rdScreen(onClickForHideBottomBar:() -> Unit){
  3. Button(onClick = { onClickForHideBottomBar()}) {
  4. Text(text = "Hide BottomBar")
  5. }
  6. }

导航图:

  1. @Composable
  2. fun NavigationGraph(onClickForHideBottomBar:() -> Unit,){
  3. NavHost(){
  4. composable(){
  5. 3rdScreen(onClickForHideBottomBar ={onClickForHideBottomBar()})
  6. }
  7. }
  8. }

支架:

  1. var bottomBarVisibility by remember { mutableStateOf(false)}
  2. Scaffold(
  3. bottomBar = {BottomNavigationView(bottomBarVisibility=bottomBarVisibility)}){
  4. NavigationGraph(onClickForHideBottomBar = bottomBarVisibility = !bottomBarVisibility){}
  5. }

底部导航视图:

  1. @Composable
  2. fun BottomNavigationView(bottomBarVisibility: Boolean){
  3. AnimatedVisibility(visibleState =MutableTransitionState(bottomBarVisibility)){
  4. BottomNavigation()
  5. }
  6. }
展开查看全部
vfwfrxfs

vfwfrxfs2#

现在可以通过合成材质3使用ModalBottomSheet。
添加依赖项:

  1. dependencies {
  2. implementation "androidx.compose.material3:material3:1.1.1"
  3. implementation "androidx.compose.material3:material3-window-size-class:1.1.1"
  4. }
  5. android {
  6. buildFeatures {
  7. compose true
  8. }
  9. composeOptions {
  10. kotlinCompilerExtensionVersion = "1.1.1"
  11. }
  12. kotlinOptions {
  13. jvmTarget = "1.8"
  14. }
  15. }

示例:

  1. @Composable
  2. fun MyScreen() {
  3. var openBottomSheet by rememberSaveable { mutableStateOf(false) }
  4. var skipPartiallyExpanded by remember { mutableStateOf(false) }
  5. val scope = rememberCoroutineScope()
  6. val bottomSheetState = rememberModalBottomSheetState(
  7. skipPartiallyExpanded = skipPartiallyExpanded
  8. )
  9. // Screen content
  10. Button(onClick = {
  11. scope.launch {
  12. openBottomSheet = !openBottomSheet
  13. }
  14. }) {
  15. Text(text = "Show Bottom Sheet")
  16. }
  17. // When openBottomSheet is true, bottom sheet is visible.
  18. if (openBottomSheet) {
  19. ModalBottomSheet(
  20. onDismissRequest = { openBottomSheet = false },
  21. sheetState = bottomSheetState,
  22. ) {
  23. //Sheet content
  24. Column(
  25. modifier = Modifier.padding(start = 16.dp, bottom = 32.dp)
  26. ) {
  27. Text("Your sheet content is here.")
  28. }
  29. }
  30. }
  31. }

closed imageopen image
注意:此API在1.1.x版本中经常发生变化。您必须参考官方API文档以获得更详细的描述和示例。

展开查看全部

相关问题