android - 将多个 LayerList 添加到 API 19 设备导致充气错误

标签 android layer-list

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

android:background="@drawable/background_profile_edit_button" + ?attr/selectableItemBackground```

完整按钮:
<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"/>

这可以通过 android:foreground="?attr/selectableItemBackground" 轻松修复但是这需要 api 23 而我的最小值是 19。

我还尝试了另一种在可绘制对象中使用图层列表的方法:
<?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>

这个层列表在我更高的 API 设备上完美运行。但是它导致我的 API 19 设备崩溃......
android.view.InflateException: Binary XML file line #201: Error inflating class TextView
这个人也有同样的问题,也没有答案:

LayerList error API 19: tag requires a 'drawable' attribute or child tag defining a drawable

TLDR:_______________________________________________________

enter image description here

在 API 19 设备上执行此操作

最佳答案

现在我只使用 RelativeLayout作为按钮的包装。如果有人有没有 NestedLayout Hacky Solution 的解决方案那很好啊。


<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/fragprofile_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background_profile_edit_button">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackground"/>
    </RelativeLayout>

</RelativeLayout>

编辑:这个问题的一个问题是 android:background="@drawable/background_profile_edit_button"在相对布局上导致角半径不起作用......在低于 API 21 的设备上......

https://stackoverflow.com/a/30692236/11110509

关于android - 将多个 LayerList 添加到 API 19 设备导致充气错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60691040/

相关文章:

android - 使用图层列表不包裹父屏幕的背景可绘制

android - 可绘制 2 种纯色

android - 递归调用 getWritableDatabase/getReadableDatabase

android - Gradle Build花费太长时间才能完成

安卓工作室 : Does not show all errors immediately

android - 使用图层列表可绘制的圆圈在 Kitkat 中不正确可见

android - 创建具有两层的自定义圆形进度条

android - 对于低于 23 的 API,图层列表中的可绘制形状呈现不正确

android - 设置 inputType textFilter 和 textPassword 停止显示为密码点而不是密码点本身

android - 一个微调器显示单选按钮,下一个不显示