如何在每行中创建一个具有不同颜色的微调器
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改为:
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")
)
)
现在,您可以根据自己的需要更改 variables/names 单词“心情”的书写方式。此外,我正在传递颜色,您可以使用 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")
)
)
我想创建一个每行颜色不同的旋转器,我知道有很多类似于我的问题的解释,但它们都在 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改为: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") ) )
现在,您可以根据自己的需要更改 variables/names 单词“心情”的书写方式。此外,我正在传递颜色,您可以使用 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")
)
)