简体   繁体   English

在 Jetpack compose 中更改被调用的可组合对象的图像

[英]Change an image of a called composable in Jetpack compose

A question, maybe a little basic since I am in an early learning phase.一个问题,可能有点基础,因为我处于早期学习阶段。 If I want to change the image of a composable when calling it, how would I do it?如果我想在调用时更改可组合的图像,我该怎么做? I know that this is possible with Int and Strings thanks to the parameters that I define in the original function, but how would it be done in the case of images?由于我在原始函数中定义的参数,我知道这对于 Int 和 Strings 是可能的,但是在图像的情况下如何完成呢?

@Composable
fun ComposableTest(exampleText:String){
    Column() {
        
        Image  (
        painter = painterResource(R.drawable.sample_image),
        contentDescription = null,
            Modifier.size(200.dp))

        //Omit the text code, it's a filler
        Text("exampleText",
            Modifier
                .background(Color.White)
                .width(200.dp)
                .padding(vertical = 30.dp), textAlign = TextAlign.Center)
    }
}

@Preview
@Composable
fun CalledTest (){
    ComposableTest(text = "Composable Text")
    
}

在此处输入图像描述

please try:请试试:

@Composable
fun ComposableTest(exampleText:String,@DrawableRes img: Int = R.drawable.sample_image){
    Column() {
        
        Image  (
        painter = painterResource(img),
        contentDescription = null,
            Modifier.size(200.dp))

        //Omit the text code, it's a filler
        Text("exampleText",
            Modifier
                .background(Color.White)
                .width(200.dp)
                .padding(vertical = 30.dp), textAlign = TextAlign.Center)
    }
}

@Preview
@Composable
fun CalledTest (){
    ComposableTest(text = "Composable Text")
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM