国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
Selector中使用顏色不成功的原因

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.android.com/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?

Thanksdb

link|edit

80% accept rate
feedback

4 Answers

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:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/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.android.com/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.android.com/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

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Android 的 selector–背景選擇器
Android L Ripple的使用
5分鐘讓你掌握Android的selector用法
Android應(yīng)用開(kāi)發(fā)筆記(10):制作自定義背景Button按鈕、自定義形狀Button的全攻略
Android SeekBar
對(duì)Android Drawable Resources的研究
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服