[英]How to create a spinner with different colors in each row
我想在每一行中创建一个具有不同颜色的旋转器,我知道有很多与我的问题类似的解释,但它们都在 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")
}
}
我想以这种方式创建我的微调器
虽然您还没有分享完整的代码,但这是您实际必须做的。
将数据模型更改为:
data class Mood(val backgroundColor: Color, val description: String)
将项目布局更改为(尽管您不需要ImageView
,但对于单个TextView
布局,您甚至不需要ConstraintLayout
,但我现在保留它):
<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>
将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 } }
最后,将适配器设置为微调器:
moodSpinner.adapter = MoodArrayAdapter( this, listOf( Mood(Color.RED, "Angry"), Mood(Color.GRAY, "Happy"), Mood(Color.CYAN, "Playful"), Mood(Color.GREEN, "Wondering") ) )
现在,您可以更改写有“心情”一词的变量/名称,因为它适合您。 此外,我正在传递颜色,您可以使用Color.ValueOf(r,g,b)
自定义颜色,或者您可以将数据模型中backgroundColor
的DataType
更改为int
并从colors.xml
传递颜色资源。
编辑 ->要为此访问颜色资源,请将其传递为:
From Activity -> Mood(resources.getColor(R.color.blue,null), "Angry")
From Fragment -> Mood(context.resources.getColor(R.color.blue,null), "Angry")
因此,相应地将您的代码更改为:
moodSpinner.adapter = MoodArrayAdapter(
this,
listOf(
Mood(resources.getColor(R.color.blue,null), "Angry"),
Mood(resources.getColor(R.color.red,null), "Happy"),
Mood(Color.CYAN, "Playful"),
Mood(Color.GREEN, "Wondering")
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.