Is it possible to reference attributes from styles.xml file?

你离开我真会死。 提交于 2019-12-04 17:06:35

Yes, it is definitely possible to add custom attributes and colors to the themes. For this you need to:

  1. Define your custom attribute in your res/values/attrs.xml file:

    <resources>
        <attr name="customColor" format="color" />
    </resources>
    
  2. Define the attribute's value in your themes:

    <style name="AppTheme" parent="Theme.AppCompat">
        <item name="customColor">#111111</item>
    </style>
    
    <style name="AppTheme.AnotherColor" parent="Theme.AppCompat">
        <item name="customColor">#222222</item>
    </style>
    
  3. Use your custom attribute in your styles:

    <style name="CustomActionBar">
        <!-- title text color -->
        <item name="android:textColorPrimary">?attr/customColor</item>
    </style>
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!