更改 arrayAdapter 中单个项目的文本颜色

change text color of singel item in arrayAdapter

我有一个带有 AutoCompleteTextView 的 TextInputLayout

我从 materials prencebels 了解到风格 :

                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"

并使用它代替微调器来显示掉落的黎明菜单 使用 ArrayAdapter

所以我的问题是我想根据位置更改列表中每个项目的textColor

我想把油变成红色,把水变成绿色?

这是我的布局:

  <com.google.android.material.textfield.TextInputLayout
                android:layout_weight="1"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
                android:id="@+id/type_Field"
                app:startIconDrawable="@drawable/type1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:hint="Type">

                <AutoCompleteTextView
                    android:id="@+id/type_auto_txt"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="none"
                    android:text="@={viewModel.type}"
                    />

和项目布局:

<TextView
android:id="@+id/dropped_item"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceSubtitle1"
/>

使用列表启动适配器的一些代码:

val types=resources.getStringArray(R.array.types)
    val typeAdapter=ArrayAdapter(requireContext(),R.layout.drop_dwon_item,types)
    binding.typeAutoTxt.setAdapter(typeAdapter)

我想用以下方法更换适配器:

1-

val arradapter=object : ArrayAdapter<String?>(requireContext(),R.layout.drop_dwon_item,types){
        override fun getDropDownView(
            position: Int,
            convertView: View?,
            parent: ViewGroup
        ): View? {

            val item: View =super.getDropDownView(position, convertView, parent)
            if (item is AutoCompleteTextView){
                if (item.text.equals("Oil")){
                    item.setTextColor(Color.RED)
                }
            }
            
            return item

        }
    }
    binding.typeAutoTxt.setAdapter(arradapter)

还有 2-

val arradapter=object : ArrayAdapter<String?>(requireContext(),R.layout.drop_dwon_item,types){
override fun getDropDownView(
    position: Int,
    convertView: View?,
    parent: ViewGroup
): View? {
    var v: View? =convertView
    val inflater=context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    v=inflater.inflate(R.layout.drop_dwon_item,null)

    val tv: TextView =v.findViewById(R.id.dropped_item)
    when(position){
        0-> tv.setTextColor(Color.RED)
        1-> tv.setTextColor(Color.GREEN)
    }

    return v
}

} binding.typeAutoTxt.setAdapter(阵列适配器)

在您的自定义适配器中使用 getView,而不是 getDropDownView,视图实际上是 MaterialTextView,但您可以只检查 TextView 是否安全边:

    // This can also be an array in your resources file or moved to companion object
    private val colors = listOf(Color.RED, Color.GREEN)

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = super.getView(position, convertView, parent)
        if (view is TextView) {
            view.setTextColor(colors[position])
        }
        return view
    }

如果您想更改所选项目的文本颜色,可以在 AutoCompleteTextView 上调用 setOnItemClickListener:

    binding.typeAutoTxt.setOnItemClickListener { _, _, position, _ ->
        binding.typeAutoTxt.setTextColor(colors[position])
    }