Android Custom Control namespace issue

匆匆过客 提交于 2020-01-02 03:00:49

问题


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> 

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:

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"

...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.

Thanks in advance!


回答1:


Ok. I got it solved!

On the original post I had:

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

...because I thought one should put the URI to the components package.

THIS IS WRONG!

On the xmlns it should be the application name as is declared on the Manifest!

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! :-)

UPDATE

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"

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.




回答2:


I had a problem similar to this, turned out it was calling a different constructor

Try with the constructor that takes in the defStyle parameter

public MyComponent(Context context, AttributeSet attrs, int defStyle) 


来源:https://stackoverflow.com/questions/9745894/android-custom-control-namespace-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!