英文:
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")
}
}
我希望以这种方式创建我的旋转器:
英文:
I would like to create a sppiner with different colors in each row, I know that there are many explanations similar to my question but they are all in Java and it has been complicated for me, I carry out the steps.
My code
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")
}
}
I want to create my spinner in this way
答案1
得分: 1
- 将数据模型更改为:
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
}
}
- 最后,将适配器设置给下拉框(spinner):
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
的数据类型更改为 int
,然后从 colors.xml
中传递颜色资源。
编辑 -> 要访问颜色资源,请按照以下方式传递:
从 Activity -> Mood(resources.getColor(R.color.blue,null), "Angry")
从 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")
)
)
英文:
You've not shared the complete code though, but here's what you actually have to do.
-
Change the data model as :
data class Mood(val backgroundColor: Color, val description: String)
-
Change the item layout as (You don't need the
ImageView
though, you don't even need theConstraintLayout
though for a singleTextView
layout, but I'm keeping it for now) :<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>
-
Change the
Adapter
class as :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 } }
-
Finally, set the adapter to the spinner as :
moodSpinner.adapter = MoodArrayAdapter( this, listOf( Mood(Color.RED, "Angry"), Mood(Color.GRAY, "Happy"), Mood(Color.CYAN, "Playful"), Mood(Color.GREEN, "Wondering") ) )
Now, you can change the variables/names where word "mood" is written as it suits you. Furthermore, I'm passing colors, you can use Color.ValueOf(r,g,b)
for custom colors or you can change the DataType
of backgroundColor
in the data model to int
and pass color resources from colors.xml
.
Edit -> To access a color resource for this, pass it as :
From Activity -> Mood(resources.getColor(R.color.blue,null), "Angry")
From Fragment -> Mood(context.resources.getColor(R.color.blue,null), "Angry")
So, change your code accordingly as :
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")
)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论