在形状 XML 中使用 attr 会导致 Android 崩溃

Ege*_*tçu 6 xml android attr

我在这个 XML 中有一个可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:endColor="@color/transparent"
        android:gradientRadius="200dp"
        android:startColor="?attr/primaryDarkTransparent"
        android:type="radial" />
</shape>
Run Code Online (Sandbox Code Playgroud)

startColor当使用以下内容时,XML 会导致崩溃?attr/primaryDarkTransparent

Caused by: java.lang.RuntimeException: org.xmlpull.v1.XmlPullParserException: <internal><gradient> tag requires 'gradientRadius' attribute with radial type Caused by: org.xmlpull.v1.XmlPullParserException: <internal><gradient> tag requires 'gradientRadius' attribute with radial type

attr戏剧性的故事是,当我使用insolid和时它运行得很好stroke,但我不知道 到底发生了什么gradient

任何建议将不胜感激。

Gio*_*oli 1

你的有两个问题shape

  1. 在 Android L (API 21) 以下,您无法在自定义可绘制对象中使用属性,因此您应该替换?attr/primaryDarkTransparentcolor引用。

  2. 应该gradientRadius是一个float. 例如200

  • 在 Android 9 (API 28) 上可以重现此问题,但前提是您使用径向渐变(线性渐变效果很好)。:( 看来 API 21 以上支持 ?attr/,除非您使用径向渐变... (4认同)