我正在尝试为我的控件创建一个自定义属性。这是我的attrs.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<declare-styleable name="ImageView">
<attr name="testcustom" format="reference"/>
</declare-styleable>
</resources>
在我的测试应用程序的根,我添加这个命名空间xmlns:TestCustom="http://schemas.android.com/apk/res-auto"
在我的布局文件后面我有一个图像视图
<ImageView
android:src="@drawable/Icon"
TestCustom:testcustom="@drawable/Icon"/>
第一个android:src属性是好的,但是TestCustom不能工作。
给出的错误是"没有找到与给定名称匹配的资源(在'testcustom',值'@drawable/icon)
所以…这是怎么回事?有人有什么想法吗?
您需要将您的xmlns:
指定为您的特定包命名空间。例如:
xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews"
表示com.example.customviews
的包命名空间为custom:
。
您可以在这里阅读更多内容:https://developer.android.com/training/custom-views/create-view.html#customattr
注意:对于自定义属性、视图等,您可能需要考虑遵循camelCase
的约定。
概括一下:这里的解决方案是在通过自定义属性引用资源时使用小写名称。显然,Xamarin在后台做了一些工作,通常允许以大写字母开头的资源是OK的,但它可能没有解决自定义属性的问题。
如果你正在使用Xamarin和自定义属性,并且你不能引用你的资源,请尝试使用小写字母!!
或者像Jon建议的那样,不要在资源中使用大写字母。