Android 自定义控件命名空间问题 [英] Android Custom Control namespace issue

查看:27
本文介绍了Android 自定义控件命名空间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为 Android 开发自定义控件,尽管我尝试执行建议的操作 此处 似乎我做错了什么.

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.

这是我的代码,看看是否有人能发现问题:

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> 

ma​​in.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"中找不到属性identifier"的资源标识符

如果我将命名空间更改为:

If I change my namespace to something like:

xmlns:bgl="http://schemas.mywhatever.com/apk/res/my.test.package"

...错误继续,事情运行但myId 为空,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.

提前致谢!

推荐答案

好的.我解决了!

在我原来的帖子上:

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 上,它应该是 Manifest 上声明的应用程序名称!

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 自定义控件命名空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆