I have an Android library with themes, attributes, styles, colors and, of course, code.
For things like separator's height, colors of items etc.. i have the ability to override dimensions using dimensions.xml and color using colors.xml (leaving the layouts, theme and style alone).
However, if i have a ListView which uses Android's list_selector_background and i also have another activity which uses this selector for a press-able TextView, besides overriding it in code, I can't find a way to do it though of using the ?style:attrib syntax but i can't see how it helps
The simple and fastest solution i currently have is create a style:
<style name="ResultsListViewStyle" >
<item name="android:listSelector">@android:drawable/list_selector_background</item>
</style>
and then copy paste the above piece of xml to my actual project style.xml and change the selector there. This is not so bad however i do need to do this for every item using this selector...
Since no one picked the glove eventually I found the solution, indeed as an attribute.
The solution:
In your attrs.xml create an entry for the value you need, for drawbale like selector for example use: <attr name="ListResultSelector" format="reference" />
In your theme.xml define your theme, if that attribute is your only one and you want Android normal theme as your theme you can use:
<style name="Theme" parent="@android:style/Theme.NoTitleBar">
<item name="ListResultSelector">@android:drawable/list_selector_background</item>
</style>
If you want to override in a specific application you can simple inherit 'theme' (<style name="Theme.newTheme">
) and change that specific property there.
In your style instead of defining a drawable you can define: <....android:background="?attrib:ListResultSelector" ../>
Apply the theme either to your application or to the relevant activity
In this way I don't need to override an entire style, only special properties like drawables, type faces etc... inside it. So if I ever need to change something in the original style and I want it to effect both the old and the new applications I don't need to do it twice.
In correction to @codeScriber's answer:
Resources defined in themes can be referenced in layouts via ?attr/ListResultSelector
or just ?ListResultSelector
.
Also refer to the docs concerning this: http://developer.android.com/guide/topics/resources/accessing-resources.html#ReferencesToThemeAttributes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With