2014-10-16 60 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);