问题描述
我一直工作在自定义控件为Android,虽然我试图做什么的建议here似乎有什么我做错了。
I've been working on a Custom Control for Android and although I tried to do what's suggested here there seems to be something I'm doing wrong.
下面是我的code,看是否有人能发现问题:
Here's my code to see if anyone can spot the problem:
MyComponent.java
public MyComponent(Context context, AttributeSet attrs)
{
super(context);
TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MyComponent);
CharSequence myId = arr.getString(R.styleable.MyComponent_identifier);
if (myId != null)
{
this.setIdentifier(myId.toString());
}
Integer cds = arr.getInteger(R.styleable.MyComponent_cd_number, 0);
if(cds != null)
{
this.setCds(cds);
}
arr.recycle();
}
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyComponent">
<attr name="cd_number" format="integer" />
<attr name="identifier" format="string" />
</declare-styleable>
</resources>
的main.xml
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bgl="http://schemas.android.com/apk/res/my.test.package.components"
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<my.test.package.MyComponent
android:id="@+id/hand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
bgl:cd_number="4"
bgl:identifier="plr"/>
...
</TableLayout>
当我把这个我收到以下错误:
When I put this I get the following errors:
错误:没有在包中my.test.package发现属性cd_number'资源标识符错误:没有在包中my.test.package
error: No resource identifier found for attribute 'cd_number' in package 'my.test.package'error: No resource identifier found for attribute 'identifier' in package 'my.test.package'
如果我改变我的名字空间是这样的:
If I change my namespace to something like:
xmlns:bgl="http://schemas.mywhatever.com/apk/res/my.test.package"
...错误走的路和东西运行,但身份识别码为空和CDS为0(默认值!)回到了MyComponent.java构造。
...the errors go way and the thing runs but myId is null and cds is 0 (the default value!) back on the MyComponent.java constructor.
我会说这是一些很基本的错误,但我不能够发现它,因为没有太多的文件在此,我决定在这里问。
I'd say it's some very basic mistake but I not being able to spot it and since there's not much documentation on this I decided to ask here.
在此先感谢!
推荐答案
确定。我把它解决了!
Ok. I got it solved!
在原来的职位我有:
xmlns:bgl="http://schemas.android.com/apk/res/my.test.package
......但在我的源代码我有:
...but in my source I had:
xmlns:bgl="http://schemas.android.com/apk/res/my.test.package.components
......,因为我认为我们应该把URI的组件包。
...because I thought one should put the URI to the components package.
这是错误的!
在的xmlns应该是已在清单中声明该应用程序名!
On the xmlns it should be the application name as is declared on the Manifest!
当我删除它匹配清单中的应用程序名称的xmlns的组件部分和错误走了,当我跑的东西在调试我真能看到我是路过的参数值在XML!
When I removed the "components" part of the xmlns it "matched" the application name in the Manifest and the errors went away and when I ran the thing in debug I could actually see the values I was passing to the parameters in the XML!
希望这可以帮助别人! : - )
Hope this helps someone else! :-)
更新
后来我有必要控制移动到库,并再次面临的问题。看来,当你把库中的元件,并用它在客户端应用程序,你必须声明的xmlns如下:
Later on I had the need to move the control into a library and faced the problem again. It seems that when you put the component in a library and use it on a client app you must declare the xmlns as below:
xmlns:myns="http://schemas.android.com/apk/res-auto"
如果你这样做(并宣布为Android的依赖库)的Eclipse(或者是Android的?)将搜索依赖适当的属性绑定。
If you do so (and have the library declared as an Android dependency) Eclipse (or is it Android?) will search the dependencies for the appropriate attribute bindings.
这篇关于Android的自定义控件的命名空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!