分享

Selector中使用颜色不成功的原因

 软件团队头目 2011-11-03

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:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas./apk/res/android">
   
<item
       
android:state_pressed="true"
       
android:color="@color/semitransparent_white"
       
/>
   
<item
       
android:color="@color/transparent"
       
/>
</selector>

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: <item> 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?

Thanks db

link|edit

80% accept rate
feedback

4 Answers

up vote 5 down vote accepted

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:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas./apk/res/android">
   
<item
       
android:state_pressed="true"
       
android:drawable="@drawable/selected_state" />
</selector>

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:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas./apk/res/android"
   
android:shape="rectangle">
   
<solid android:color="@color/semitransparent_white" />
</shape>

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
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

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:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   
<drawable name="semitransparent_white">#77ffffff</drawable>
</resources>

And then use as such in your selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas./apk/res/android">
   
<item
       
android:state_pressed="true"
       
android:drawable="@drawable/semitransparent_white" />
</selector>
link|edit
feedback

which res folder have you kept your selector in??

link|edit
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

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

link|edit
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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多