Android TextView 背景色的选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3592780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selector on background color of TextView
提问by digitalbreed
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:
我试图TextView
在用户触摸它时更改 Android小部件的背景颜色。我为此创建了一个选择器,它存储在res/color/selector.xml
大致如下所示的位置:
<?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.
的clickable
属性TextView
是true
,以防万一。
When I assign this selector to a TextView
as android:background="@color/selector"
, I'm getting the following exception at runtime:
当我将此选择器分配给TextView
as 时android:background="@color/selector"
,我在运行时收到以下异常:
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
错误/AndroidRuntime(13130):由:org.xmlpull.v1.XmlPullParserException:二进制 XML 文件第 6 行:标签需要“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).
当我将属性更改为 drawable 时,它可以工作,但结果看起来完全错误,因为 ID 似乎被解释为图像引用而不是颜色引用(如“drawable”所暗示的那样)。
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.
令我困惑的是,我可以直接设置颜色参考,例如“@color/black”作为背景属性。这按预期工作。使用选择器不起作用。
I can also use the selector as the textColor
without problems.
我也可以textColor
毫无问题地使用选择器。
What's the correct way to apply a background-color-selector to a TextView
in Android?
TextView
在 Android 中将背景颜色选择器应用于 a 的正确方法是什么?
回答by Benoit Martin
The problem here is that you cannot define the background color using a color selector, you need a drawableselector. 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.
您还需要将该资源移动到drawable
更有意义的目录,因为它本身不是颜色选择器。
Then you would have to create the res/drawable/selected_state.xml
file like this:
然后你必须res/drawable/selected_state.xml
像这样创建文件:
<?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.
注意:OP 获取图像资源的原因可能是因为他试图仅引用仍在颜色目录中但使用的资源,@drawable
因此最终导致 ID 冲突,选择了错误的资源。
Hope this can still help someone even if the OP probably has, I hope, solved his problem by now.
希望这仍然可以帮助某人,即使 OP 现在可能已经解决了他的问题。
回答by azdev
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:
Benoit 的解决方案有效,但您确实不需要承担绘制形状的开销。由于颜色可以绘制,只需在 /res/values/colors.xml 文件中定义颜色:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="semitransparent_white">#77ffffff</color>
</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="@color/semitransparent_white" />
</selector>
回答by Jason Robinson
An even simpler solution to the above:
对上述问题的更简单的解决方案:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<color android:color="@color/semitransparent_white" />
</item>
<item>
<color android:color="@color/transparent" />
</item>
</selector>
Save that in the drawable folder and you're good to go.
将其保存在 drawable 文件夹中,您就可以开始了。
回答by Maksim Dmitriev
Even this works.
即使这有效。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/dim_orange_btn_pressed" />
<item android:state_focused="true" android:drawable="@color/dim_orange_btn_pressed" />
<item android:drawable="@android:color/white" />
</selector>
I added the android:drawable
attribute to each item, and their values are colors.
我android:drawable
为每个项目添加了属性,它们的值是颜色。
By the way, why do they say that color
is one of the attributes of selector
? They don't write that android:drawable
is required.
顺便说一句,为什么他们说这color
是 的属性之一selector
?他们不写那android:drawable
是必需的。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="hex_color"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>
回答by Dasser Basyouni
For who is searching to do it without creating a background sector, just add those lines to the TextView
对于在不创建背景扇区的情况下进行搜索的人,只需将这些行添加到 TextView
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
Also to make it selectable use:
还要使其可选择使用:
android:textIsSelectable="true"