gpt4 book ai didi

android - 下拉菜单填充

转载 作者:行者123 更新时间:2023-12-04 23:53:54 24 4
gpt4 key购买 nike

在 compose 中制作下拉菜单时,我遇到了一个问题,即我的 DropdownMenu 将始终按修饰符中的方式填充 MaxWidth。
我的目标是添加填充,使其与屏幕的内容相匹配,但是向修饰符添加填充不起作用......

@Composable
fun PriorityDropDown(
priority: Priority,
onPrioritySelected: (Priority) -> Unit
) {

var expanded by remember { mutableStateOf(false) }
val dropDownIconAngle: Float by animateFloatAsState(targetValue = if (expanded) 0f else -90f)

Row(
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colors.background)
.height(PRIORITY_DROPDOWN_HEIGHT)
.clickable { expanded = true }
.border(
width = 1.dp,
shape = MaterialTheme.shapes.small,
color = MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.disabled)
),
verticalAlignment = Alignment.CenterVertically
) {
Canvas(
modifier = Modifier
.size(PRIORITY_INDICATOR_SIZE)
.weight(1.5f),
onDraw = {
drawCircle(color = priority.color)
}
)

Text(
text = priority.name,
style = MaterialTheme.typography.subtitle2,
modifier = Modifier.weight(8f)
)

IconButton(
onClick = { expanded = true },
modifier = Modifier.weight(2f)
) {
Icon(
imageVector = Icons.Filled.ArrowDropDown,
contentDescription = stringResource(R.string.drop_down_arrow),
modifier = Modifier
.alpha(ContentAlpha.medium)
.rotate(dropDownIconAngle)
)
}

DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
modifier = Modifier
.fillMaxWidth()
) {
// ...
}
}
}
没有填充是什么感觉:

硬编码是什么感觉 .fillMaxWidth(fraction = 0.942f)

最佳答案

添加var rowSize by remember { mutableStateOf(Size.Zero) }到行修饰符添加:

.onGloballyPositioned { layoutCoordinates ->
rowSize = layoutCoordinates.size.toSize()
}
然后到 DropDown 修改器:
.width(with(LocalDensity.current) { rowSize.width.toDp() })
解决了问题

关于android - 下拉菜单填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69939854/

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