如何在Jetpack Compose中获取上下文 [英] How to get Context in Jetpack Compose

查看:1104
本文介绍了如何在Jetpack Compose中获取上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fun createListItem(itemIndex: Int) {
Padding(left = 8.dp, right = 8.dp, top = 8.dp, bottom = 8.dp) {
    FlexRow(crossAxisAlignment = CrossAxisAlignment.Center) {
        expanded(1.0f) {
            Text("Item $itemIndex")
        }
        inflexible {
            Button(
                "Button $itemIndex",
                style = ContainedButtonStyle(),
                onClick = {
                    Toast.makeText(
                        this@MainActivity,
                        "Item name $itemIndex",
                        Toast.LENGTH_SHORT
                    ).show()
                })

        }
    }
  }
}

我尝试用普通的方式做烤面包.但是我遇到了错误,我尝试了很多倍数源,但是失败了.

I try to make Toast in a normal way. but I got the error I tried a lot of multiples source but failed.

推荐答案

您可以使用定义ambientContext访问上下文.

You can access to context with define ambientContext.

示例:

val context = +ambient(ContextAmbient)

这篇关于如何在Jetpack Compose中获取上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆