向 API 19 设备添加多个 LayerList 导致膨胀错误

DIR*_*AVE 6 android layer-list

我正在尝试创建一个具有两个背景的按钮,我的自定义背景

android:background="@drawable/background_profile_edit_button" + ?attr/selectableItemBackground```
Run Code Online (Sandbox Code Playgroud)

完整按钮:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/fragprofile_constraint"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:background="@drawable/background_profile_edit_button"
    android:paddingStart="32dp"
    android:paddingTop="6dp"
    android:paddingEnd="32dp"
    android:paddingBottom="6dp"
    android:text="Edit Profile"
    android:textColor="@color/colorBlackFont"
    android:textSize="12sp"
    android:textStyle="bold"/>
Run Code Online (Sandbox Code Playgroud)

这可以很容易地解决,android:foreground="?attr/selectableItemBackground"但是需要 api 23 而我的最小值是 19。

我还尝试了另一种在 drawable 中使用 layer-list 的方法:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="rectangle">
            <solid android:color="@color/colorWhite" />

            <stroke
                android:width="1dp"
                android:color="@color/dividerColor" />
            <corners android:radius="3dp"/>
        </shape>
    </item>

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


</layer-list>
Run Code Online (Sandbox Code Playgroud)

此图层列表在我的更高 API 设备上完美运行。但是它导致我的 API 19 设备崩溃......

android.view.InflateException: Binary XML file line #201: Error inflating class TextView

这个人也有同样的问题,也没有得到答复:

LayerList 错误 API 19:标签需要“drawable”属性或定义可绘制对象的子标签

TLDR:_________________________________________________________

在此处输入图片说明

在 API 19 设备上执行此操作

tyn*_*ynn 1

和都是在 Android SDK level 21 中引入的。之前Context.getDrawable(Theme)Resources.getDrawable(int,Theme)可绘制资源中不支持主题属性。只是无法Theme从中加载属性。

Alex Lockwood 的博客文章《使用主题属性设置颜色和绘图样式》对此进行了更好的解释。