2017-03-07 90 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

是嗎?這會是一個問題嗎? –