Unable to add attribute primary color in android drawable xml

I have my theme, defined as

<style name="AppThemeRed" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimaryRed</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDarkRed</item>
        <item name="colorAccent">@color/colorAccentRed</item>
    </style>

In my XML layouts, I do

<android.support.design.widget.AppBarLayout
        android:background="?attr/colorPrimary"
        android:id="@+id/topBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

Whenever I change any topic, colorPrimary changes

However, if I have the same thing that is added to drawable, for example,

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="?attr/colorPrimary" />
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="?attr/colorPrimary" />
            <corners android:radius="5dp"/>
        </shape>
    </item>
</selector>

Failure in the inability to inflate a view, a view that has a background set as @drawabe/xxxx

How can I define the theme color attribute in my XML drawable

+4
source share
4 answers

just replace ...

android:color="?attr/colorPrimary"

to ...

android:color="@color/colorPrimaryRed"

Update

It is not possible to refer to an attribute in the xml output below API 21. To make your theme you need:

xml . @color #RGB.

drawable attrs.xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Attributes must be lowercase as we want to use them for drawables -->
    <attr name="my_drawable" format="reference" />
</resources>

drawable theme.xml.

<style name="MyTheme" parent="@android:style/Theme.NoTitleBar">
   <item name="my_drawable">@drawable/my_drawable</item>
</style>

, .

<TextView android:background="?my_drawable" />
+3

<solid android:color="@android:attr/colorPrimary"/>

<solid android:color="?attr/colorPrimary" />
0

: -

<solid android:color="@android:color/holo_orange_dark"  />
0

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


All Articles