2014-10-16 62 views
1

我有子類的偏好,並在attrs.xml創建的自定義styleables如下:如何通過派生類訪問Android內部樣式屬性?

<declare-styleable name="MyPreference"> 
    <attr name="myAttribute" format="reference" /> 
</declare-styleable> 

,我可以在XML聲明我的偏好如下:

<com.my.project.MyPreference 
    android:title="myTitle" 
    namespace:myAttribute="@array/sleep_time_values" /> 

我可以訪問自定義屬性剛罰款:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyPreference, 0, 0); 
thing = a.getText(R.styleable.MyPreference_myAttribute); 

我的問題是:如何訪問(在代碼中)在上述xml定義中指定的內部android:title屬性?

回答

0

框架可修改陣列不是公共(或穩定)的,但是您可以使用相同的方法獲取單個公共屬性來獲取樣式屬性。這應該僅用於獲取單個屬性,因爲資源框架期望屬性數組按特定順序排列。

TypedArray a = context.obtainStyledAttributes(
    attrs, new int[] { android.R.attr.title }, 0, 0); 
String title = a.getText(0);