我一直在开发一个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”中找不到属性“identifier”的资源标识符
如果我将命名空间更改为类似于:
xmlns:bgl="http://schemas.mywhatever.com/apk/res/my.test.package"
我尝试修复错误,让程序能够正常运行,但是在MyComponent.java构造函数中,myId为空且cds为0(默认值!)。
我认为这可能是一些非常基本的错误,但我没有发现它。由于文档不多,所以我决定在这里问问。
先感谢您!