How to change text color of preference category in Android?

前端 未结 11 538
别那么骄傲
别那么骄傲 2020-12-05 04:28

The textColor attribute isn\'t working. Here\'s my XML:


         


        
11条回答
  •  旧巷少年郎
    2020-12-05 04:52

    Inspired by @AliSh answer, but I needed to only change the colour of one Preference text item. So, for all the Kotlin guys out there:

    class TextColorPreference : Preference {
    
        constructor(context: Context) : super(context)
    
        constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    
        constructor(
            context: Context, attrs: AttributeSet,
            defStyle: Int
        ) : super(context, attrs, defStyle)
    
        override fun onBindViewHolder(holder: PreferenceViewHolder?) {
            super.onBindViewHolder(holder)
    
            context?.let {
                (holder?.findViewById(android.R.id.title) as? TextView)?.setTextColor(
                    ContextCompat.getColor(
                        it,
                        R.color.colorPrimary
                    )
                )
            }
        }
    }
    

    And then put it in your xml/prefs.xml:

    
    
    
        
    
    

提交回复
热议问题