I've jumped around looking for a proper solution to this problem, but can't find one. The answer might be that it's impossible, but I'm going to try one more time, even though many similar questions have been asked.
I have a view (it happens to be a RatingBar, but it could be a button or a TextView or even an ImageView) that I want a custom style for. Depending on something, I want it to (for example) use Red Stars, or Blue Stars, or Orange Stars. I want to use this view in a list, with a custom list adapter, so that based on some attribute in the underlying list data, the item list view shows either开发者_JAVA百科 the Red-Star RatingBar, or the Blue-Star RatingBar, or the OrangeStar RatingBar.
I've attempted building a style from code (choosing the right drawables that reflect the color stars I want), and then having the custom ListAdapter create a new RatingBar with the appropriate style (since a view's style can't be changed after the view is created), but when I do that the RatingBar never shows up. I'm not sure if this is expected, or if I have something else wrong.
It's like I want a selector for my image selector, or maybe another (custom?) attribute for the selector I already have.
If I create a CustomView, that extends RatingBar, that has an extra attribute, would an xml-defined Selector be able to access that attribute and use it to select a different image?
I'd like to figure this out, because the only alternative I can come up with is to not use a ListView with a ListAdapter, which basically results in me writing more custom code.
Thoughts?
Have you tried using themes? You can define a theme for the RatingBar in styles.xml. Within this RatingBar theme you can define a android:progressDrawable, which will refer to a drawable. Create multiple themes, that each point to a different drawable. Having these different themes, you will be able to programmatically set the RatingBar Widget android:style attribute to support these themes (red, blue, green) that you create.
精彩评论