选择在TextView的背景颜色颜色、背景、TextView

2023-09-12 01:00:03 作者:保质期

我试图当用户触摸它来改变一个Android的背景颜色的TextView 小部件。我创建了一个选择,为此目的,存储在 RES /彩色/ selector.xml 和大致的样子说:

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>

点击属性的TextView ,如果这就是兴趣。

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

当我将这个选择到的TextView 安卓背景=@色/选择器,我敢在运行时得到以下异常:

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

ERROR / AndroidRuntime(13130​​):org.xmlpull.v1.XmlPullParserException:二进制XML文件,6号线:由造成标签需要一个'绘制'属性或子标签定义绘制

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

当我更改属性来绘制的,它的工作原理,但结果看完全错误的,因为标识似乎是PTED为图像引用,而不是颜色引用除$ P $(为绘制建议)。

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.

什么是在Android的背景颜色选择器适用于的TextView 的正确方法?

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

推荐答案

这里的问题是,你不能使用颜色选择器中定义的背景颜色,你需要绘制选择。因此,必要的修改是这样的:

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.

然后,你就必须创建RES /像这样绘制/ selected_state.xml文件:

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"

注意:为什么OP越来越绘制的图像资源的原因可能是因为他试图只是引用了他的资源,还是在色彩目录,但使用@drawable让他结束了一个ID冲突,选择了错误的资源。

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可能有,我希望,现在解决了他的问题。

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