Using theme references in XML drawables requires API level 21

北战南征 提交于 2019-12-10 14:33:23

问题


Is there a way to make following code compatible with lower API levels:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="?android:attr/selectableItemBackground"/>

    <item android:gravity="bottom">
        <shape android:shape="rectangle">
            <size android:height="1px"/>
            <solid android:color="#ccc"/>
        </shape>
    </item>

</layer-list>

The part: android:drawable="?android:attr/selectableItemBackground" produces this message:

Using theme references in XML drawables requires API level 21 (current min is 15)


回答1:


Create an attars.xml file under the values folder.

In the attars.xml file, create an item for selectableItemBackground:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="selectableItemBackground" format="reference" />
</resources>

In your styles.xml define this attribute with your drawable:

<item name="selectableItemBackground">@drawable/yourDrawable</item>

And then in your drawable file, reference the attribute:

<item android:drawable="?attr/selectableItemBackground"/>



回答2:


use android:drawable="?attr/selectableItemBackground"



来源:https://stackoverflow.com/questions/41650820/using-theme-references-in-xml-drawables-requires-api-level-21

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!