如何创建每行具有不同颜色的微调器

时间:2020-09-13 00:06:44

标签: java android kotlin spinner android-spinner

我想创建一个每行具有不同颜色的sppiner,我知道有很多与我的问题类似的解释,但是它们都是Java的,对于我来说这很复杂,我执行这些步骤。

我的代码

val lista = listOf<Mood>(
    Mood(resources.getColor(R.color.blue, null), "Color1"),
    Mood(resources.getColor(R.color.purple, null), "Color2"),
    Mood(resources.getColor(R.color.green, null), "Color3"),
    Mood(resources.getColor(R.color.darkred, null), "Color4")
)

val adaptador = MoodArrayAdapter(this, lista)
spinner1.adapter = adaptador

spinner1.onItemSelectedListener = object :
    AdapterView.OnItemSelectedListener {
    override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
        when (spinner1.selectedItem.toString()) {
            "Color1" -> textView.setBackgroundResource(R.color.blue)
            "Color2" -> textView.setBackgroundResource(R.color.purple)
            "Color3" -> textView.setBackgroundResource(R.color.green)
            "Color4" -> textView.setBackgroundResource(R.color.darkred)
        }
    }
    override fun onNothingSelected(p0: AdapterView<*>?) {
        TODO("Not yet implemented")
    }
}

我想以此方式创建我的微调器

enter image description here

1 个答案:

答案 0 :(得分:1)

尽管您尚未共享完整的代码,但这实际上是您要做的。

  1. 将数据模型更改为:

    data class Mood(val backgroundColor: Color, 
        val description: String)
    
  2. 将项目布局更改为(尽管对于单个ImageView布局,您甚至不需要ConstraintLayout,甚至不需要TextView,但是我'暂时保留它):

    <android.support.constraint.ConstraintLayout
        android:id="@+id/rootLayout"
        ...>
        <TextView
            android:id="@+id/moodText"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"/>
    </android.support.constraint.ConstraintLayout>
    
  3. Adapter类更改为:

    class MoodArrayAdapter(ctx: Context,
        moods: List<Mood>) :
        ArrayAdapter<Mood>(ctx, 0, moods) {
        override fun getView(position: Int, recycledView: View?, parent: ViewGroup): View {
            return this.createView(position, recycledView, parent)
        }
        override fun getDropDownView(position: Int, recycledView: View?, parent: ViewGroup): View {
            return this.createView(position, recycledView, parent)
        }
        private fun createView(position: Int, recycledView: View?, parent: ViewGroup): View {
            val mood = getItem(position)
            val view = recycledView ?: LayoutInflater.from(context).inflate(
                R.layout.demo_spinner,
                parent,
                false
            )
            view.rootLayout.setBackgroundColor(mood.backgroundColor)
            view.moodText.text = mood.description
            return view
        }
    }
    
  4. 最后,将微调器的适配器设置为:

    moodSpinner.adapter = MoodArrayAdapter(
        this,
        listOf(
            Mood(Color.RED, "Angry"),
            Mood(Color.GRAY, "Happy"),
            Mood(Color.CYAN, "Playful"),
            Mood(Color.GREEN, "Wondering")
        )
    )
    

现在,您可以更改适合您的单词“ mood”的变量/名称。此外,我正在传递颜色,您可以将Color.ValueOf(r,g,b)用于自定义颜色,也可以将数据模型中DataType的{​​{1}}更改为backgroundColor并从中传递颜色资源int

编辑-> 要访问此颜色资源,请以:p

colors.xml

因此,将您的代码相应地更改为:

From Activity -> Mood(resources.getColor(R.color.blue,null), "Angry")
From Fragment -> Mood(context.resources.getColor(R.color.blue,null), "Angry")