您可以在 Jetpack Compose 预览中展开 DropdownMenu 吗?

Can You Expand a DropdownMenu in the Jetpack Compose Preview?

我有以下创建 TopAppBar 的可组合函数。预览显示栏很好,但我也可以在预览中扩展菜单吗?

@Composable
@Preview
fun AppBarTop(refreshOnClickHandler: (() -> Unit)? = null) {
    var showMenu by remember { mutableStateOf(false) }

    TopAppBar(
        elevation = 10.dp,
        title = {
            Text(stringResource(R.string.app_name))
        },
        actions = {
            IconButton(onClick = { /* //TODO Make this */ }) {
                Icon(
                    Icons.Filled.Search,
                    contentDescription = stringResource(R.string.content_desc_search_icon)
                )
            }
            IconButton(onClick = { showMenu = !showMenu }) {
                Icon(
                    Icons.Filled.MoreVert,
                    contentDescription = stringResource(R.string.content_desc_menu_icon)
                )
            }
            DropdownMenu(
                expanded = showMenu,
                onDismissRequest = { showMenu = false }
            ) {
                DropdownMenuItem(onClick = { /*TODO*/ }) {
                    Text(stringResource(R.string.action_refresh))
                }
                DropdownMenuItem(onClick = { /*TODO*/ }) {
                    Text(stringResource(R.string.action_settings))
                }
            }
        }
    )
}

想通了。交互式预览是一项实验性功能,因此您必须先在 Android Studio 设置中启用它。

File -> Settings -> Experimental 然后选中 Enable interactive and animation preview tools.

旁边的框

完成后,交互式预览按钮将出现在组件预览的正上方。