【问题标题】:Drawable not beign fond in kotlin file在 kotlin 文件中找不到可绘制对象
【发布时间】:2020-09-08 17:52:17
【问题描述】:

我正在尝试从非 Activity 类访问可绘制资源,这是因为我想将我的可组合函数放在另一个文件中,而不是将它放在主文件中,问题是我可以从主文件中访问 R.drawable.header ,但不能从另一个装箱文件中访问

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.Text
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumnFor
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.unit.dp
import androidx.ui.tooling.preview.Preview
import com.example.jetexample.ui.typography

@Composable
fun RecipeCard(recipe: Recipe){
    val image = imageResource(R.drawable.header)
    Surface(shape = RoundedCornerShape(8.dp),elevation = 8.dp,modifier = Modifier.padding(8.dp)) {
        Column(modifier = Modifier.padding(16.dp)) {
            val imageModifier = Modifier.preferredHeight(150.dp).fillMaxWidth().clip(shape = RoundedCornerShape(8.dp))
            Image(asset = image,modifier = imageModifier,contentScale = ContentScale.Crop)
            Spacer(modifier = Modifier.preferredHeight(16.dp))
            Text(text = recipe.title,style = typography.h6)
            for(ingredient in recipe.ingredients){
                Text(text = ingredient,style = typography.body2)
            }
        }
    }
}

我知道它与上下文有关,这就是为什么我无法访问我认为的资源,但我尝试使用 ContextAmbient 仍然无法

【问题讨论】:

    标签: android kotlin android-recyclerview android-jetpack-compose


    【解决方案1】:

    检查R.drawable 是否指向您项目中的可绘制资源文件夹。如我所见,您尚未在文件中导入资源。

    import com.example.jetexample.R
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多