How to use resource arrays using xml in Android?

后端 未结 2 820
长发绾君心
长发绾君心 2020-12-16 11:58

I am new in Android development and facing a problem with managing Android resources. I want to create a listView with an ImageView and a TextView.

Following is my i

2条回答
  •  一整个雨季
    2020-12-16 12:17

    You can use resources from res/values/arrays.xml.

    For drawables

    
        @drawable/ic_active_image
        @drawable/ic_visited_image
    
    
    val drawables = resources.obtainTypedArray(R.array.your_images)
    val drawable = drawables.getResourceId(position, -1)
    image.setImageResource(drawable)
    drawables.recycle()
    

    For colors

    
        #365374
        #00B9FF
    
    
    val colors = resources.obtainTypedArray(R.array.your_colors)
    val color = colors.getColor(position, -1)
    title.setTextColor(color)
    colors.recycle()
    

    For strings

    
        Active
        Visited
    
    
    val strings = resources.getStringArray(R.array.your_strings)
    title.text = strings[position]
    

    Plurals:

    
        No proposals
        %1$d proposal
        %1$d proposals
        %1$d proposals
        %1$d proposals
        %1$d proposals
    
    
    val proposals = count.takeIf { it != 0 }?.let {
        resources.getQuantityString(R.plurals.proposal_plurals, it, it)
    } ?: "No proposals available"
    

提交回复
热议问题