如何修复包括气球(给出奇怪的结果)在JetPack组合中的约束Layout?

发布于 2025-02-02 06:24:44 字数 1348 浏览 4 评论 0原文

我正在尝试在我的第一个应用程序中进行帮助。进行弹出工作后出现的问题是,我正在使用的图标变成一个按钮占用整个屏幕高度。 我正在使用我找到的

@Composable
fun GiveHelp(helpText: String) {
Surface{
    val context = LocalContext.current
    val lifecycleOwner = LocalLifecycleOwner.current

    ConstraintLayout {
        val (icon, text) = createRefs()
        Icon(
            modifier = Modifier
                .constrainAs(icon) {
                    top.linkTo(parent.top)
                    start.linkTo(parent.start)
                },
            painter = painterResource(id = R.drawable.ic_help),
            contentDescription = "help Icon"
        )
        Text(
            modifier = Modifier
                .constrainAs(text) {
                    top.linkTo(icon.top)
                    start.linkTo(icon.end)
                    bottom.linkTo(icon.bottom)
                }
                .padding(horizontal = 10.dp),
            text = "Is your task:"
        )
        BalloonAnchor(
            reference = icon,
            modifier = Modifier
                .aspectRatio(0.1f),
            balloon = BalloonUtils.getTitleBalloon(
                context = context,
                title = helpText,
                lifecycle = lifecycleOwner
            ),
            onAnchorClick = { balloon, anchor -> balloon.showAlignTop(anchor) }
        )
    }
}
} 

I'm trying to make a help pop ups in my first app. the problem that came up after making the pop up work is that the icon which I'm using becomes a button taking up whole screen height.
I'm using the only code I found for balloon popups in jetpack compose.
the layout is fine until I add the BalloonAnchor.
this is the code:

@Composable
fun GiveHelp(helpText: String) {
Surface{
    val context = LocalContext.current
    val lifecycleOwner = LocalLifecycleOwner.current

    ConstraintLayout {
        val (icon, text) = createRefs()
        Icon(
            modifier = Modifier
                .constrainAs(icon) {
                    top.linkTo(parent.top)
                    start.linkTo(parent.start)
                },
            painter = painterResource(id = R.drawable.ic_help),
            contentDescription = "help Icon"
        )
        Text(
            modifier = Modifier
                .constrainAs(text) {
                    top.linkTo(icon.top)
                    start.linkTo(icon.end)
                    bottom.linkTo(icon.bottom)
                }
                .padding(horizontal = 10.dp),
            text = "Is your task:"
        )
        BalloonAnchor(
            reference = icon,
            modifier = Modifier
                .aspectRatio(0.1f),
            balloon = BalloonUtils.getTitleBalloon(
                context = context,
                title = helpText,
                lifecycle = lifecycleOwner
            ),
            onAnchorClick = { balloon, anchor -> balloon.showAlignTop(anchor) }
        )
    }
}
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏夜暖风 2025-02-09 06:24:44

这里的问题是您在 balloonanchor 的修饰符中使用的 expackratio 。尝试modifier.aspectratio(0.99f)之类的东西。使用此功能,您的图标不会占据整个屏幕高度。
,您可以使用以下代码之类的东西来获得理想的外观。

BalloonAnchor(
    reference = icon,
    modifier = Modifier
        .height(40.dp),
    balloon = BalloonUtils.getTitleBalloon(
        context = context,
        title = helpText,
        lifecycle = lifecycleOwner
    ),
    onAnchorClick = { balloon, anchor -> balloon.showAlignTop(anchor) }
)

The problem here is the aspectRatio you are using in the Modifier of BalloonAnchor. Try something like Modifier.aspectRatio(0.99f). Using this, your Icon will not take the entire screen height.
Or, your can use something like below code to get a desirable look.

BalloonAnchor(
    reference = icon,
    modifier = Modifier
        .height(40.dp),
    balloon = BalloonUtils.getTitleBalloon(
        context = context,
        title = helpText,
        lifecycle = lifecycleOwner
    ),
    onAnchorClick = { balloon, anchor -> balloon.showAlignTop(anchor) }
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文