如何通过派生类访问Android内部样式属性?

如何通过派生类访问Android内部样式属性?,android,attr,Android,Attr,我对preference进行了子类化,并在attrs.xml中创建了自定义样式表,如下所示: <declare-styleable name="MyPreference"> <attr name="myAttribute" format="reference" /> </declare-styleable> <com.my.project.MyPreference android:title="myTitle" namespac

我对preference进行了子类化,并在attrs.xml中创建了自定义样式表,如下所示:

<declare-styleable name="MyPreference">
    <attr name="myAttribute" format="reference" />
</declare-styleable>
<com.my.project.MyPreference
    android:title="myTitle"
    namespace:myAttribute="@array/sleep_time_values" />

我的问题是:如何访问(在代码中)上述xml定义中指定的内部android:title属性?

框架可样式化数组不是公共的(或稳定的),但您可以使用获取样式化属性的相同方法获取单个公共属性。这只能用于获取单个属性,因为资源框架希望属性数组按特定顺序排列

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