gpt4 book ai didi

android - 在 Jetpack compose 中添加抽屉切换按钮

转载 作者:行者123 更新时间:2023-12-03 13:34:05 42 4
gpt4 key购买 nike

我想将汉堡图标添加到 Appbar 并使用该图标打开/关闭抽屉。

我将如何实现这一目标?

Scaffold(
drawerShape = RoundedCornerShape(topRight = 10.dp, bottomRight = 10.dp),
drawerElevation = 5.dp,
drawerContent = {
// Drawer
},
topBar = {
TopAppBar(
navigationIcon = {
Icon(
Icons.Default.Menu,
modifier = Modifier.clickable(onClick = {
// Open drawer => How?
})
)
},

modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(bottomLeft = 10.dp, bottomRight = 10.dp)),
title = { Text(text = "Hello") }
)
},
) {}

最佳答案

使用 rememberScaffoldState() 修改抽屉状态。

  • 创建一个变量:
  • val state = rememberScaffoldState()
    val scope = rememberCoroutineScope() // in 1.0.0-beta `open()` and `close` are suspend functions
  • 将状态传递给 Scaffold
  • Scaffold(
    scaffoldState = state,
    // ...
    )
  • 使用state.drawerState.open()state.drawerState.close()onClick打开/关闭抽屉。
  • navigationIcon 创建一个图标在 TopAppBar :
  • val state = rememberScaffoldState()
    Scaffold(
    scaffoldState = state,
    topBar = {
    TopAppBar(
    title = { Text(text = "AppBar") },
    navigationIcon = {
    Icon(
    Icons.Default.Menu,
    modifier = Modifier.clickable(onClick = {
    scope.launch { if(it.isClosed) it.open() else it.close() }
    })
    )
    }
    )
    },
    drawerShape = RoundedCornerShape(topRight = 10.dp, bottomRight = 10.dp),
    drawerContent = {
    Text(text = "Drawer")
    }
    ) {
    // Scaffold body
    }

    关于android - 在 Jetpack compose 中添加抽屉切换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63738585/

    42 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com