How to get Context in Jetpack Compose

前端 未结 7 1660
情歌与酒
情歌与酒 2021-02-19 07:35
fun createListItem(itemIndex: Int) {
Padding(left = 8.dp, right = 8.dp, top = 8.dp, bottom = 8.dp) {
    FlexRow(crossAxisAlignment = CrossAxisAlignment.Center) {
               


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-19 07:37

    For getting context in jetpack compose:

    val context = ContextAmbient.current
    

    Working on 0.1.0-dev14

    How to use it in TOAST:

    @Composable
    fun cardViewImplementer(item: Int) {
       val context = ContextAmbient.current
       Card(
         shape = RoundedCornerShape(10.dp),
         modifier = Modifier.padding(10.dp)
       ) {
         Box(
            modifier = Modifier
                .fillMaxWidth()
                .drawShadow(5.dp)
                .clickable(onClick = {
                    Toast.makeText(context, "Clicked $item", Toast.LENGTH_SHORT).show()
                }), children = {
    
            })
    }
    

    For accessing the Resource:

    Text("Read this string: "+context.getString(R.string.name))
    

提交回复
热议问题