我有一个自定义弹出窗口:
if (expanded) {
Popup(
onDismissRequest = {},
properties = PopupProperties(
clippingEnabled = false
),
popupPositionProvider = object : PopupPositionProvider {
override fun calculatePosition(
anchorBounds: IntRect,
windowSize: IntSize,
layoutDirection: LayoutDirection,
popupContentSize: IntSize
): IntOffset {
return IntOffset(
x = textXPosition,
y = barYPosition
)
}
}
) {
Text(
text = "This is the popup content.",
modifier = Modifier.background(Color.White)
)
}
}
式中,textXPosition和barYPosition的取值如下:
textXPosition:
var textXPosition by remember { mutableStateOf(0) }
Text(
text = state.currentFileName,
modifier = Modifier
.weight(1f)
.padding(start = 8.dp)
.clickable { expanded = !expanded }
.onGloballyPositioned { coordinates ->
textXPosition = coordinates.positionInRoot().x.toInt()
}
)
if (expanded) {...}
barYPosition:
var barYPosition by remember {
mutableStateOf(0)
}
ActionBar(
modifier = Modifier
.height(36.dp)
.background(Color(0xfffafafa))
.onGloballyPositioned { coordinates ->
barYPosition = coordinates.positionInRoot().y.toInt()
}
...)
我最后得到的是图片上的:
这个 idea 是弹出窗口将直接在文本上方,但实际上有一个小缺口,我不知道为什么..