【问题标题】:Custom Android attribute without namespace没有命名空间的自定义 Android 属性
【发布时间】:2013-01-11 07:24:27
【问题描述】:

我正在自定义LayoutInflater.Factory,并且在 onCreateView() 方法中我想获取为当前视图指定的自定义属性。但是我不想为此属性声明样式。如果在此视图的 xml 属性中指定了此参数,我可以使用: attrs.getAttributeValue(null, attributeName);
如果在 View 的 style 参数中指定它,我什至可以使用: context.obtainStyledAttributes(attrs, new int[]{R.attr.custom_attribute);
但是,如果在主题中指定了此属性,似乎不可能获得它。任何帮助将不胜感激。

这是我的 xml 资源:

<style name="CustomTheme"
       parent="android:Theme.DeviceDefault">
     <item name="android:textViewStyle">@style/TextView.Custom</item>
</style>

<style name="TextView.Custom">
    <item name="custom_attribute">"blabla"</item>
</style>

附:我知道我应该为自定义属性声明 slyleable 资源,如果我这样做了,它会起作用,但我想确保它是不可能的,因为使用这种方式更简单。

【问题讨论】:

    标签: android


    【解决方案1】:

    原来你可以用这个得到它:

    TypedValue value = new TypedValue();
    theme.resolveAttribute(android.R.attr.textViewStyle, value, true);
    typedArray = theme.obtainStyledAttributes(value.resourceId, new int[] {R.attr.custom_attribute});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      • 2014-07-21
      • 2017-07-16
      • 2010-11-04
      • 2015-04-09
      相关资源
      最近更新 更多