Android color selector doesn't work with custom attributes

后端 未结 3 1687
误落风尘
误落风尘 2020-12-06 00:10

I\'ve got in attrs.xml

 
    
    
    <         


        
相关标签:
3条回答
  • 2020-12-06 00:44

    You cannot reference ?attr/ when choosing colors for a selector. What you can do, if you want per-theme colors in your selector, is create multiple selectors which reference @color/ and @drawable/, and then have a "reference" attr which associates one of the selectors with the given style.

    <attr name="forground_to_background" format="reference" />

    You then have to set the text color like

    android:textColor="?attr/forground_to_background"
    

    I believe the text was always red because Android was interpreting the attr's integer value as a color (red), rather than using it as a lookup for what you actually wanted.

    0 讨论(0)
  • 2020-12-06 00:45

    The reason why this happens is that I have different Context. While inflating Context is aware of my theme attrs, but to the ListView adapter I passed ApplicationContext that wasn't aware of those attrs. Now I don't know why it doesn't know about them ;)

    0 讨论(0)
  • 2020-12-06 01:01

    Are you sure if you applying MyTheme to the activity or the textview? Another thing you can try is that instead of using the "?" operator in your forground_to_background.xml, trying using "@" instead. see if that fixes your problem

    0 讨论(0)
提交回复
热议问题