无法在可绘制对象中使用主题属性。得到了android.content.res.Resources $ NotFoundException错误

问题描述 投票:0回答:2

我得到了以下可绘制形状:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="#222222" />
    <stroke android:width="1dip" android:color="?attr/colorPrimary"/>
    <corners
        android:topLeftRadius="58dp"
        android:topRightRadius="58dp"
        android:bottomRightRadius="58dp"
        android:bottomLeftRadius="58dp">
    </corners>
    <padding
        android:left="4dp"
        android:top="4dp"
        android:right="4dp"
        android:bottom="4dp" />
</shape>

我正在此行中通过主题设置颜色属性:

<stroke android:width="1dip" android:color="?attr/colorPrimary"/>

项目编译正常,没有任何问题,但是在设备上运行时,出现以下问题:

    android.view.InflateException: Binary XML file line #119: Error inflating class TextView
    ...
    Caused by: android.content.res.Resources$NotFoundException: File
 res/drawable/bordered_green_solid_textview.xml from drawable resource ID #0x7f07006a

当我替换?attr / colorPrimary并使用例如#22​​2222这样的十六进制颜色时,它将毫无问题地运行。

我该如何在可绘制对象中使用?attrs而不出现问题?

P.S。:我的最低API级别19

我的T​​extView

<TextView android:id="@+id/imagesCounter"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="35dp"
            android:layout_marginBottom="35dp"
            android:background="@drawable/bordered_green_solid_textview"
            android:gravity="center"
            android:text="16"
            android:textColor="?attr/colorPrimary"
            android:textSize="9sp"
            app:layout_constraintBottom_toBottomOf="@+id/imageBorder"
            app:layout_constraintStart_toStartOf="@+id/imageBorder"
            tools:text="67" />
android styles themes android-drawable
2个回答
0
投票

是从颜色值中使用颜色或使用十六进制颜色的更好方法。如果您必须使用?attr值:

  1. 您应该定义颜色
  2. 创建文件res / value / attrs.xml
  3. 以一种样式定义颜色并为您的属性设置值
  4. 在atdraw中使用attr

0
投票

为什么必须使用?attr

只需通话

android:color="?colorPrimary"
© www.soinside.com 2019 - 2024. All rights reserved.