当前位置: 首页 > 工具软件 > urban-theme > 使用案例 >

Resources.Theme

孟彦
2023-12-01
public final class

Resources.Theme

extends  Object
java.lang.Object
   ↳ android.content.res.Resources.Theme

Class Overview


This class holds the current attribute values for a particular theme. In other words, a Theme is a set of values for resource attributes; these are used in conjunction with TypedArray to resolve the final value for an attribute.

该类保存有当前主题的所有属性值。换句话说,该类是 resource 属性值集合;为了获得一个属性的最终的值,它常和 TypedArray 联合使用。

The Theme's attributes come into play in two ways: (1) a styled attribute can explicit reference a value in the theme through the "?themeAttribute" syntax; (2) if no value has been defined for a particular styled attribute, as a last resort we will try to find that attribute's value in the Theme.

主题的属性值主要通过两种方式起作用:1.styled 属性可以通过 "?themeAttribute" 这种语法明确引用一个主题的属性值。2. styled attribute 属性没有被定义一个值,最后将在 theme 中查找改值。

You will normally use the obtainStyledAttributes(AttributeSet, int[], int, int) APIs to retrieve XML attributes with style and theme information applied.

您常常会使用到 obtainStyledAttributes(AttributeSet, int[], int, int) 来检索通过 style 和 theme 应用的 xml 属性。

public TypedArray obtainStyledAttributes (AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)

Added in  API level 1

Return a TypedArray holding the attribute values in set that are listed in attrs. In addition, if the given AttributeSet specifies a style class (through the "style" attribute), that style will be applied on top of the base attributes it defines.

返回一个 TypedArray 来保存 attrs 列表中的属性值。另外,如果给定的 AttributeSet 通过 style 属性指定了一个 style 类,这个 style 将被应用在它定义的基础属性的最上面。

Be sure to call TypedArray.recycle() when you are done with the array.

当使用结束后一定要调用 TypedArray.recycle() 来回收相关的资源(比如 xml parser 等等)。

When determining the final value of a particular attribute, there are four inputs that come into play:

  1. Any attribute values in the given AttributeSet.
  2. The style resource specified in the AttributeSet (named "style").
  3. The default style specified by defStyleAttr and defStyleRes
  4. The base values in this theme.

     一个属性的最终值由 4 种输入方式决定:
  1. AttributeSet 中的任何属性值
  2. AttributeSet 中通过 style 属性指定的 style 资源
  3. 通过 defStyleAttr 和 defStyleRes 指定的值
  4. 主题中的基本值

Each of these inputs is considered in-order, with the first listed taking precedence over the following ones. In other words, if in the AttributeSet you have supplied <Button textColor="#ff000000">, then the button's text will always be black, regardless of what is specified in any of the styles.

上面值的优先级是自顶向下逐渐变弱的。1最高,4最弱,换句话说,1和4都指定了某个属性值,最终生效的是1中的值。

Parameters
set The base set of attribute values. May be null.
attrs The desired attributes to be retrieved.
defStyleAttr An attribute in the current theme that contains a reference to a style resource that supplies defaults values for the TypedArray. Can be 0 to not look for defaults.
defStyleRes A resource identifier of a style resource that supplies default values for the TypedArray, used only if defStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.
Returns
  • Returns a TypedArray holding an array of the attribute values. Be sure to call TypedArray.recycle() when done with it.

 类似资料:

相关阅读

相关文章

相关问答