2017-03-07 89 views
1

我做了一个库,我想包括在我的项目中。自定义属性返回null

在这方面,我有这个在我的attr.xml:

<declare-styleable name="MyCustomView"> 
    <attr name="MyPrivateID" format="string"/> 
</declare-styleable> 

我包括我的应用程序库,并能正常工作。但我不能检索我的自定义属性。

这是我activity_main.xml中

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:custom="http://schemas.android.com/apk/res-auto" 
...........> 

<something.myown.MyCustomView 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
custom:MyPrivateID="GjM4X9QfK2VT05BKlEyffkAKGLU=dhbdsljhbdDjTchvNTQwMg=="/> 

而且在MyCustomView.java,我有:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView); 
try { 
    String string = a.getString(R.styleable.MyCustomView_MyPrivateID); 
    return string; 
} finally { 
    a.recycle(); 
} 

不知何故,返回字符串为空。任何帮助表示赞赏。

回答

0

你能改变吗?

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HalalAdView); 

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView); 

EDITED

把这个样本中的样子。它的工作https://github.com/betorcs/SampleCustomView

+0

改变它,这就是我跑的代码是 –

+0

您是否试图在构造函数中获得MyPrivateID值? – betorcs

+0

是吗?这会是一个问题吗? –