[英]How to add a theme attribute to android:startColor in gradient drawable in Android?
According to android documentation, android:startColor can take attributes as a value: 根据android文档,android:startColor可以将属性作为值:
This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.
这也可能是对资源(格式为“@ [package:] type:name”)或主题属性(格式为“?[package:] [type:] name”)的引用,其中包含此类型的值。
I am trying to add an attribute to my gradiant drawable, however I get an error. 我试图为我的gradiant drawable添加一个属性,但是我得到一个错误。 Here is the code for the drawable:
这是drawable的代码:
Here is the code in style.xml: 这是style.xml中的代码:
<style name="test" parent="android:Theme">
<item name="android:startColor">#0b2749</item>
<item name="startColor">#0b2749</item>
</style>
When I try to run the activity I get this error message: 当我尝试运行活动时,我收到以下错误消息:
01-10 20:47:30.810: E/AndroidRuntime(7279): Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
I tried changing ?startColor to ?attr/startColor and still got the same error. 我尝试将?startColor更改为?attr / startColor并仍然遇到相同的错误。 I also applied the theme to the activity in the AndroidManifest.xml file.
我还将主题应用于AndroidManifest.xml文件中的活动。
How can I add a theme attribute to the gradient drawable? 如何将主题属性添加到渐变可绘制的?
I was having the same problem in one of my drawable in which I want different colors based on the chosen theme. 我在我的一个drawable中遇到了同样的问题,我根据所选主题想要不同的颜色。 I found a workaround, that doesn't exactly solve the problem, but works for me.
我找到了一个解决方法,但并没有完全解决问题,但对我有用。 Instead of defining the color in my styles, I define the whole drawable as a reference.
我没有定义样式中的颜色,而是将整个drawable定义为引用。
In attrs.xml
: 在
attrs.xml
:
<resources>
<attr name="myDrawable" format="reference"/>
<resources>
And in my style.xml
: 在我的
style.xml
:
<style name="style1">
<item name="myDrawable">@drawable/myDrawable1</item>
<style>
<style name="style2">
<item name="myDrawable">@drawable/myDrawable2</item>
<style>
Of course, you need to define two drawables in your drawable folder, myDrawable1
and myDrawable2
, each one having the correct color hard-coded. 当然,您需要在可绘制文件夹
myDrawable1
和myDrawable2
定义两个drawable,每个文件都具有硬编码的正确颜色。
And it works fine. 它工作正常。
<style name="customTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
</style>
Add the above code in colors.xml in values folder and the following code in styles.xml 在values文件夹中的colors.xml中添加上面的代码,在styles.xml中添加以下代码
<color name="titlebackgroundcolor">#FFFFFF</color>
<color name="titletextcolor">#000000</color>
Looks like you just made a small mistake for your color. 看起来你只是犯了一个小错误的颜色。 Android takes the
#[alpha] [hex]
type of input for colors. Android采用
#[alpha] [hex]
类型的颜色输入。 You just provided the hex part. 你刚刚提供了十六进制部分。 Android doesn't support this, hence the
UnsupportedOperationException
. Android不支持此功能,因此
UnsupportedOperationException
。
Try using this: 试试这个:
<item name="android:startColor">#FF0b2749</item>
I can't guarantee that this works though, as I have never worked with gradients in combination with a style. 我无法保证这是有效的,因为我从未使用渐变与风格相结合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.