Updated 18 December 2016
In this blog,
We have shown you how to make a custom attribute and how to reference color attribute in drawable.
You can define attributes in the top <resources> element or inside of an <declare-styleable>element.
Note: All attributes share the same global namespace. That means that even if you create a new attribute inside of an
<declare-styleable>element it can be used outside of it and you cannot create another attribute with the same name of a different type.
An <attr> element has two XML attributes name and format. name lets you call it something and this is how you end up referring to it in code, e.g.,R.attr.my_c_attribute The format attribute can have different values depending on the ‘type‘ of the attribute you want.
There are few simple steps,
|
1 2 3 4 5 |
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="my_link_color1">#0077CC</color> <color name="my_link_color2">#626262</color> </resources> |
|
1 2 3 4 |
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="myLinkColor" format="reference" /> </resources> |
Theme1 and Theme2) define:|
1 2 3 4 5 6 7 |
<style name="Theme1" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="myLinkColor">@color/my_link_color1</item> </style> <style name="Theme2" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="myLinkColor">@color/my_link_color2</item> </style> |
|
1 |
android:textColor="?attr/myLinkColor" |
Maybe this blog is helpable for you.
Source: https://github.com/android/
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
2 comments