android themes - defining colours in custom themes

后端 未结 1 1321
一个人的身影
一个人的身影 2020-11-29 05:35

I am sure there is a simple answer to that yet I just cant find it so I throw it into stackoverflow ... ;-)

I will just put it into an example. I have an android app

相关标签:
1条回答
  • 2020-11-29 05:41

    I found a solution which seems to work. First you need to define the custom color fields in attr.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
    <attr name="titleColor" format="reference|color" />
    <attr name="introColor" format="reference|color" />
    
    </resources>
    

    Next you define your themes

    <style name="AppTheme.MyDark" parent="android:Theme">
       <item name="titleColor">#FFFFFF</item>
       <item name="introColor">#FFFFFF</item>
    </style>
    
    
    <style name="AppTheme.MyLight" parent="android:Theme">
       <item name="titleColor">#000000</item>
       <item name="introColor">#004444</item>
    </style>
    

    and finally in your layout

    <TextView
        android:id="@+id/quoteTitle"
        android:textColor="?titleColor"
        ...
    </TextView>
    
    <TextView
        android:id="@+id/quoteIntro"
        android:textColor="?introColor"
        ...
    </TextView>
    

    i found the solution mainly here

    There seems to be no explanation in the official android documentation about using attributes. Best resource I found is here

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