186

我创建了一个自定义小部件,并在 layout.xml 中声明它。我还在 attr.xml 中添加了一些自定义属性。但是,当尝试在 styles.xml 中以样式声明这些属性时,它给了我No resource found that matches the given name: attr 'custom:attribute'.

我已将xmlns:custom="http://schemas.android.com/apk/res/com.my.package"所有标签放入 styles.xml 中,包括<?xml><resources><style>,但它仍然给我同样的错误,它找不到我的自定义 XML 命名空间。

但是,我可以使用我的命名空间手动为 layout.xml 中的视图分配属性,因此命名空间没有任何问题。我的问题在于让styles.xml 知道我的attr.xml。

4

6 回答 6

368

我想到了!答案是不要在样式中指定命名空间。

<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="CustomStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>

        <item name="custom_attr">value</item> <!-- tee hee -->
    </style>
</resources>
于 2011-07-29T03:23:46.743 回答
44

上面的答案对我有用,我尝试了一点改变,我在资源元素中声明了一个类的样式。

<declare-styleable name="VerticalView">
    <attr name="textSize" format="dimension" />
    <attr name="textColor" format="color" />
    <attr name="textBold" format="boolean" />
</declare-styleable>

declare-styleable中,name属性引用了一个类名,所以我有一个视图类调用“com.my.package.name.VerticalView”,它表示这个声明必须在 VerticalView 或 VerticalView 的子类中使用。所以我们可以像这样声明样式:

<resources>
    <style name="verticalViewStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">36dip</item>

        <item name="textSize">28sp</item>  <!-- not namespace prefix -->
        <item name="textColor">#ff666666</item>
        <item name="textBold">true</item>
    </style>
</resources>

这就是为什么我们没有在资源元素中声明命名空间,它仍然有效。

于 2013-05-07T07:23:04.297 回答
17

值/样式.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
    <item name="defaultButtonColor">@color/red</item>
    <item name="defaultButtonHeight">@dimen/dp_100</item>
</style>

值/attrs.xml

<resources>
    <attr name="defaultButtonColor" format="reference" />
    <attr name="defaultButtonHeight" format="reference"/>
</resources>

值/colors.xml

<resources>
    <color name="red">#f00</color>
</resources>

值/尺寸.xml

<resources>
    <dimen name="dp_100">100dp</dimen>
</resources>

使用

<Button
    android:layout_width="wrap_content"
    android:layout_height="?attr/defaultButtonHeight"
    android:text="Button"
    android:textColor="?attr/defaultButtonColor"
    />

在此处输入图像描述

演示

于 2017-12-22T09:06:39.207 回答
10

Styler 和 vince 的修改对我有用。我想指出@vince 的解释可能并不完全准确。

declare-styleable为了测试与自定义视图类的名称匹配的名称属性允许我们在没有命名空间的情况下访问自定义属性的假设,我更改了名称declare-styleable(自定义视图被命名为TestViewFont

<declare-styleable name="TextViewFont2">
    <attr name="font" format="integer"/>
</declare-styleable>

然后我更改了obtainStyledAttributes自定义视图中的调用以反映这一点:

TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TextViewFont2, 0, 0);

代码仍然运行。declare-styleable所以我不认为这是对它命名的类的某种内省。

因此,我被引导相信任何自定义属性都可以用于声明样式而无需引用命名空间。

无论如何,感谢所有帮助人员,它解决了我的问题。

于 2013-10-30T16:15:00.710 回答
5
  • 定义一些属性

<declare-styleable name="refreshPullRefreshLayout">
        <attr name="refreshColors" format="reference"/>
        <attr name="refreshColor" format="reference"/>
</declare-styleable>
  • 在布局文件中使用它

<com.aolphn.PullRefreshLayout
     app:refreshColor="@color/main_green"
     app:refreshColors="@array/refresh_color"/>
  • 最后在样式文件中使用它

    样式文件和布局文件的区别是我们不添加前缀app:
<style name="refreshStyle">
    <item name="refreshColor">@color/main_green</item>
    <item name="refreshColors">@array/refresh_color</item>
</style>

试试看,祝你有美好的一天,这对我有用。

于 2019-07-11T07:54:09.570 回答
2

万一它对其他人有帮助,我的错误是我的自定义视图类正在调用AttributeSet.getAttributeValue例如

String fontName = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "customFont");

...这导致我的自定义属性没有被我的自定义视图读取。

修复是obtainStyledAttributes在我的自定义视图中使用:

 TypedArray styleAttrs = context.obtainStyledAttributes(attrs, R.styleable.MyTextViewStyleable);
 String fontName = styleAttrs.getString(R.styleable.MyTextViewStyleable_customFont);

提示这是正常工作的,您可以 Ctrl/Apple + 单击R.styleable.MyTextViewStyleable_customFont以直接进入 attrs.xml 定义。

我花了一段时间才发现我的代码和其他示例之间的关键区别,因为自定义属性在直接通过布局 XML(而不是通过样式)传入时工作正常。

于 2014-12-13T02:52:09.367 回答