Repeat using gradient with themed colors

In two different actions I want to use the same gradient, but with different colors. I think in order to convey the colors of the gradient in the theme of activity as follows:

I added the following lines to attrs.xml

<attr name="backgroundTopColor" format="color" />
<attr name="backgroundBottomColor" format="color" />

in bg_gradient.xml I typed

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="?backgroundTopColor" 
        android:endColor="?backgroundBottomColor" 
        android:angle="270" />
    <corners android:radius="0dp" />
</shape>

In the activity topic, I added

<item name="backgroundTopColor">#FFFFFFFF</item>
<item name="backgroundBottomColor">#FFFFFF00</item>

After running the application in logcat

02-07 14: 03: 59.479: ERROR/AndroidRuntime (2096): : java.lang.UnsupportedOperationException: : type = 0x2
02-07 14: 03: 59.479: ERROR/AndroidRuntime (2096): at android.content.res.TypedArray.getColor(TypedArray.java:326)
02-07 14: 03: 59.479: ERROR/AndroidRuntime (2096): at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:647)
02-07 14: 03: 59.479: ERROR/AndroidRuntime (2096): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
02-07 14: 03: 59.479: ERROR/AndroidRuntime (2096): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
02-07 14: 03: 59.479: /AndroidRuntime (2096): at android.content.res.Resources.loadDrawable(Resources.java:1694)
02-07 14: 03: 59.479: ERROR/AndroidRuntime (2096):... 29

?

NB: API = 8

+3
1

"?attr/backgroundTopColor" "?backgroundTopColor" .

: , ? :

<item name="backgroundTopColor">#FFFFFFFF</item>

:

<item name="backgroundTopColor">@color/myColor</item>

colors.xml :

<color name="myColor">#FFFFFFFF</color>

, , . , , , , , .

0

Source: https://habr.com/ru/post/1790460/


All Articles