【问题标题】:How to create a spinner with different colors in each row如何在每行中创建具有不同颜色的微调器
【发布时间】:2020-09-13 00:06:44
【问题描述】:

我想在每一行创建一个具有不同颜色的spiner,我知道有很多类似于我的问题的解释,但它们都是用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")
    }
}

我想以这种方式创建我的微调器

【问题讨论】:

  • 检查 this Kotlin 的示例。使用数据模型将颜色传递为backgroundColor,并在示例中使用的微调器的项目布局中将根布局分配为rootLayout,然后在AdaptercreateView() 中,设置传递的颜色数组的各自项目到rootLayoutview.rootLayout.setBackgroundColor(mood.backgroundColor)。这就是您所需要的,您可以根据需要进一步定制解决方案,首先按照我告诉您的方式实施它,然后您就会知道该做什么以及如何做。
  • 我创建了一个叫spinner的values资源文件,在constraintlayout比这个create默认我已经赋值android: id = "@ + id / rootLayout" android: background = "@ color / colorPrimaryDark" 最后在private fun createView(position: Int, recycledView: View ?, parent: ViewGroup): View我把这行代码@987654334 @但我红了backgroundColor如果我错了,我很抱歉并请求您的大力帮助
  • 默认情况下。您不必像在此处所做的那样对颜色资源进行硬编码:android:background= "@color/colorPrimaryDark"。您只需在CreateView() 中设置颜色。接下来,您如何将这些值添加到数据类中?添加代码,您可能只传递一种颜色:红色。你要做的是你必须通过不同的颜色和每个不同的值来获得不同的颜色。并在问题中添加代码并通过评论通知我。
  • 分享代码,因为我认为您将颜色分配给微调器本身,而不是项目行。再次检查链接,您必须设置每行根布局的背景颜色,显示为here
  • 我已经添加了我的代码,其余代码和教程一样

标签: java android kotlin spinner android-spinner


【解决方案1】:

虽然您还没有分享完整的代码,但这是您实际必须做的事情。

  1. 将数据模型更改为:

    data class Mood(val backgroundColor: Color, 
        val description: String)
    
  2. 将项目布局更改为(虽然您不需要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>
    
  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")
        )
    )
    

现在,您可以根据自己的需要更改写入“心情”一词的变量/名称。另外,我是传颜色的,你可以用Color.ValueOf(r,g,b)自定义颜色,也可以把数据模型中backgroundColorDataType改成int,从colors.xml传颜色资源。

Edit -> 要为此访问颜色资源,请将其传递为:

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")
    )
)

【讨论】:

  • 它对我有用,非常感谢,但我还有一个问题,我在colors.xml中创建了这个颜色 # FF33B5E5 @color / blue 我尝试在 MainActivity 中像这样 Mood (Color.blues, "Angry") 或在 Mood 数据类中调用它 val backgroundColor = (R.color.blues ) 在这两个部分我都得到一个错误你能告诉我我应该如何编写和编码来自定义颜色
  • @JorgeLeonardo 检查我已经更新了使用颜色资源的答案。
  • 我有另一个问题,明智的老师,我如何才能在选择其中一行时文本视图改变颜色,我尝试按如下方式进行,但我做不到,我有更新了问题。
  • @JorgeLeonardo 为此,您必须使用onItemSelectedListener()。在onItemSelected() 中,您可以将TextView 的颜色更改为textView.setBackgroundColor(color)。要根据选定的项目更改它,您可以执行var selectedItem = (parent?.selectedItem as Mood) 然后比较选定的 Mood 值是否是您要通过if(selectedItem.description.equals("YourChoice") 更改颜色的值,如果是,则更改 TextView 的颜色。这可以通过多种方式完成。
猜你喜欢
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 2019-02-20
  • 1970-01-01
相关资源
最近更新 更多