【问题标题】:Kotlin Jetpack, How to load Icon images using string arrayKotlin Jetpack,如何使用字符串数组加载图标图像
【发布时间】:2022-11-04 14:40:58
【问题描述】:

我正在使用以下代码将图像从 drawables 文件夹加载到按钮。

                     Icon(
                            painter=painterResource(R.drawable.imageName),
                            modifier=Modifier.size(30.dp),
                            contentDescription="drawable icons",
                            tint=Color.Unspecified
                        )

但我想在循环中使用该代码与字符串数组,例如

         val imageNames = arrayOf("image1", "image2")

            for (k in imageNames.indices) {

                      Icon(
                            painter=painterResource(R.drawable.imageNames[k]),
                            modifier=Modifier.size(30.dp),
                            contentDescription="drawable icons",
                            tint=Color.Unspecified
                        )
              }        

【问题讨论】:

    标签: android kotlin android-jetpack-compose


    【解决方案1】:

    在您的情况下,可绘制对象应该是可绘制值资源而不是 String

    val imageNames = arrayOf("image1", "image2")
    

    应该

    val imageRes = arrayOf(R.drawable.ic_1, R.drawable.ic_2)
    
    imageRes.forEach { res ->
         Icon(
             painter=painterResource(res),
             modifier=Modifier.size(30.dp),
             contentDescription="drawable icons",
             tint=Color.Unspecified
         )
    }
    

    但如果您想将image1image2 字符串值映射到相应的Drawables,请考虑这一点,

    @Composable
    fun MyScreen() {
    
        val imageNames = arrayOf("image1", "image2")
    
        imageNames.forEach { imageString ->
            val imageRes = imageString.mapToMyImageResource()
    
            Icon(
                painter=painterResource(imageRes),
                modifier=Modifier.size(30.dp),
                contentDescription="drawable icons",
                tint=Color.Unspecified
            )
        }
    }
    
    @DrawableRes
    fun String.mapToMyImageResource() : Int =
        when (this) {
            "image1" -> {
                R.drawable.ic_1
            }
            "image2" -> {
                R.drawable.ic_2
            }
            else -> {
                R.drawable.ic_default
            }
        }
    

    【讨论】:

      【解决方案2】:
      @Composable
      fun ImageList() {
      
      //declare your list as resource types
      val imagesNames = arrayOf(R.drawable.image1,R.drawable.image2)
      
      Column {
          imageNames.forEach { image ->
             Icon(
                     painter=painterResource(image),
                     modifier=Modifier.size(30.dp),
                     contentDescription="drawable icons",
                     tint=Color.Unspecified
                  )
           }
       }
      }
      

      希望对您有所帮助!

      【讨论】:

        猜你喜欢
        • 2023-02-11
        • 2014-07-13
        • 1970-01-01
        • 2016-09-29
        • 2019-11-08
        • 1970-01-01
        • 2019-04-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多