【问题标题】:How do I access an Android internal style attributes via derived classes?如何通过派生类访问 Android 内部样式属性?
【发布时间】:2014-10-16 23:56:45
【问题描述】:

我有子类偏好,并在 attrs.xml 中创建了自定义样式,如下所示:

<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 属性?

【问题讨论】:

    标签: android attr


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      相关资源
      最近更新 更多