武汉哪个站离欢乐谷近:Selector中使用颜色不成功的原因

来源:百度文库 编辑:偶看新闻 时间:2024/04/28 00:55:58

Selector on background color of TextView

up vote 2 down vote favorite 2 share [fb] share [tw]

I'm attempting to change the background color of an Android TextView widget when the user touches it. I've created a selector for that purpose, which is stored in res/color/selector.xml and roughly looks like that:



            android:state_pressed="true"
        android:color="@color/semitransparent_white"
        />
            android:color="@color/transparent"
        />

The clickable attribute of the TextView is "true", in case that's of interest.

When I assign this selector to a TextView as android:background="@color/selector", I'm getting the following exception at runtime:

ERROR/AndroidRuntime(13130): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #6:  tag requires a 'drawable' attribute or child tag defining a drawable

When I change the attribute to drawable, it works, but the result is looking completely wrong because the IDs appear to be interpreted as image references instead of color references (as the "drawable" suggests).

What confuses me is that I can set a color reference, e.g. "@color/black", as the background attribute directly. This is working as expected. Using selectors doesn't work.

I can also use the selector as the textColor without problems.

What's the correct way to apply a background-color-selector to a TextView in Android?

Thanksdb

android link|edit edited Aug 28 '10 at 22:46
asked Aug 28 '10 at 22:40digitalbreed
13219
80% accept rate feedback

4 Answers

activeoldestvotes up vote 5 down voteaccepted

I don't usually answer questions that are several months old but it's bothering me that this question is staying unanswered.

The problem here is that you cannot define the background color using a color selector, you need a drawable selector. So, the necessary changes would look like this:



            android:state_pressed="true"
        android:drawable="@drawable/selected_state" />

You would also need to move that resource to the drawable directory where it would make more sense since it's not a color selector per se.

Then you would have to create the res/drawable/selected_state.xml file like this:


    android:shape="rectangle">
   

and finally, you would use it like this:

android:background="@drawable/selector"

Note: the reason why the OP was getting an image resource drawn is probably because he tried to just reference his resource that was still in the color directory but using @drawable so he ended up with an ID collision, selecting the wrong resource.

Hope this can still help someone even if the OP probably has, I hope, solved his problem by now

link|edit answered Mar 14 at 6:38Benoit Martin
437310 Thanks, Benoit. The problem was solved (I must admit, I can't remember how exactly I did it in the end) and the project was successfully finished. I appreciate that you came back here to post and help people hitting the same problem in the future, great spirit! – digitalbreed Mar 24 at 0:10 I can't make this work. I'm trying to apply it to a button and it does set the background to the default color of the selector, but it doesn't change to the shape defined in state_pressed. What could I be missing? I might open a new question, just in case you could point me in the right direction. – Maragues Apr 14 at 18:00 @Maragues it's hard to tell without seeing any code. I'd recommend you open a new question and post the relevant code so we can figure out what could be wrong. You can add a comment to this post with a link to your new post too. – Benoit Martin Apr 14 at 21:20 feedback up vote 0 down vote

Benoit's solution works, but you really don't need to incur the overhead to draw a shape. Since colors can be drawables, just define a color in a /res/values/colors.xml file:



    #77ffffff

And then use as such in your selector:



            android:state_pressed="true"
        android:drawable="@drawable/semitransparent_white" />

link|edit answered yesterdayazdev
658613 feedback up vote 0 down vote

which res folder have you kept your selector in??

link|edit answered Aug 29 '10 at 10:35Umesh
1,27429 It's in res/color/selector.xml. – digitalbreed Aug 29 '10 at 10:53 well i have used the @drawable method and its working fine.i'll try to dig deep when i get some time. – Umesh Aug 30 '10 at 5:19 Questions should be directed to the comments section. Putting a question as an 'answer' only clutters the page. – Treebranch Mar 4 at 19:31 feedback up vote 0 down vote

A color can be interpreted as a drawable. How is the result wrong exactly?

link|edit answered Aug 29 '10 at 1:40Romain Guy
18.3k11735 It's not showing the color but an image from my drawable resources as the background instead. – digitalbreed Aug 29 '10 at 3:00 feedback

Your Answer