我一直在为Android设计一个自定义控件,虽然我试图做什么建议here似乎有什么我做错了。Android自定义控件命名空间问题
这里是我的代码,看看是否有人能发现问题:
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>
当我把这个我得到以下错误:
错误:未发现在包“my.test.package” 错误属性“cd_number”资源标识符:没有资源标识符发现属性“标识”中包“my.test.package”
如果我改变我的命名空间是这样的:
xmlns:bgl="http://schemas.mywhatever.com/apk/res/my.test.package"
...错误去W¯¯ ay和东西运行,但myId为null,并且cds为0(默认值!)回到MyComponent.java构造函数上。
我想说这是一个非常基本的错误,但我无法发现它,因为没有太多的文档,我决定在这里问。
在此先感谢!
伟大的工作研究和提供答案。 res-auto修复正是我所需要的。 – AlanKley 2013-04-15 16:16:01
'res-auto'。你美丽! – darksider474 2014-05-08 09:35:27
什么是bgl和myns,我怎么设定这个名字 – Nepster 2015-01-08 13:01:15