首页 > 解决方案 > 错误:错误:未找到与给定名称匹配的资源(用于属性参考)

问题描述

我的 account_particle.xml 布局中有这一行

            android:padding="?attrs/disc_padding"

这个 attrs.xml:

<resources>
  <declare-styleable name="AccountParticle">
    <attr name="apStyle" format="enum">
      <enum name="header" value="0"/>
      <enum name="list_item" value="1"/>
    </attr>

    <attr name="text_margin_start" format="reference"/>
    <attr name="text_margin_end" format="reference"/>
    <attr name="text_margin_right" format="reference"/>
    <attr name="text_margin_left" format="reference"/>
    <attr name="disc_padding" format="reference"/>
    <attr name="disc_imageViewSize" format="reference"/>


  </declare-styleable>
</resources>

这个styles.xml:

<style name="Theme.ap.header" parent="Theme.AppCompat">
    <item name="disc_padding">@dimen/account_menu_header_signed_in_avatar_margin_start</item>
  </style>

  <style name="Theme.ap.list_item" parent="Theme.AppCompat">
    <item name="disc_padding">@dimen/account_menu_account_list_item_avatar_margin_start</item>



      </style>

为什么我会收到此编译错误?

 error: Error: No resource found that matches the given name (at 'padding' with value '?attrs/disc_padding').

帐户粒子

标签: androidandroid-layoutstylescustom-attributes

解决方案


该问题与应该使用的属性用法有关,?attr/而不是?attrs/.


但是您的attrs.xml文件中还有另一个问题。问题是您在 a 中声明了以下引用declare-styleable

<attr name="text_margin_start" format="reference"/>
<attr name="text_margin_end" format="reference"/>
<attr name="text_margin_right" format="reference"/>
<attr name="text_margin_left" format="reference"/>
<attr name="disc_padding" format="reference"/>
<attr name="disc_imageViewSize" format="reference"/>

此引用应移出declare-styleable

为什么

declare-styleable用于定义attrs将在自定义视图的上下文中使用的一组。在您的情况下,在disc_paddinga 的上下文之外使用AccountParticle.

文档参考:https ://developer.android.com/training/custom-views/create-view#customattr


推荐阅读