public class ColorStateList
extends Object implements Parcelable
| java.lang.Object | |
| android.content.res.ColorStateList | |
让您将 View状态集映射为颜色。
ColorStateList是从应用程序资源目录的“color”子目录中定义的XML资源文件创建的。 XML文件包含一个单独的“选择器”元素,其中包含多个“item”元素。 例如:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:color="@color/sample_focused" />
<item android:state_pressed="true"
android:state_enabled="false"
android:color="@color/sample_disabled_pressed" />
<item android:state_enabled="false"
android:color="@color/sample_disabled_not_pressed" />
<item android:color="@color/sample_default" />
</selector>
This defines a set of state spec / color pairs where each state spec specifies a set of states that a view must either be in or not be in and the color specifies the color associated with that spec.
每个项目定义一组状态规格和颜色对,其中状态规范是一系列设置为true或false的属性以表示包含或排除。 如果没有为某个项目指定属性,则可能是任何值。
例如,每当设置了关注状态时,下列项目都会匹配; 任何其他状态可能被设置或取消设置:
<item android:state_focused="true"
android:color="@color/sample_focused" />
通常,颜色状态列表将引用框架定义的状态属性,例如android:state_focused或android:state_enabled ; 但是,也可以使用应用程序定义的属性。
注意:状态规格列表将按照它们出现在XML文件中的顺序进行匹配。 出于这个原因,更具体的项目应该放在文件的早期位置。 没有状态规范的项目被认为与任何一组状态匹配,并且通常作为用作缺省的最终项目是有用的。
如果没有状态规格的物品放置在其他物品之前,这些物品将被忽略。
每个项目必须定义一个 android:color属性,该属性可以是HTML样式的十六进制颜色,对颜色资源的引用,或者 - 在API 23及更高版本中 - 可解析为颜色的主题属性。
从API 23开始,项目可以选择定义一个android:alpha属性来修改基本颜色的不透明度。 该属性采用介于0和1之间的浮点值或可以解析的主题属性。 项目的整体颜色是通过将基本颜色的Alpha通道乘以alpha值来计算的。 例如,以下项目表示50%不透明度下主题的重音颜色:
<item android:state_enabled="false"
android:color="?android:attr/colorAccent"
android:alpha="0.5" />
有关更多信息,请参阅 Color State List Resource的指南。
XML attributes |
|
|---|---|
android:alpha |
Alpha multiplier applied to the base color. |
android:color |
Base color for this state. |
Inherited constants |
|---|
android.os.Parcelable
|
Fields |
|
|---|---|
public static final Creator<ColorStateList> |
CREATOR |
Public constructors |
|
|---|---|
ColorStateList(int[][] states, int[] colors) 创建一个ColorStateList,它返回从状态到颜色的指定映射。 |
|
公共方法(Public methods) |
|
|---|---|
static ColorStateList |
createFromXml(Resources r, XmlPullParser parser) 此方法在API级别23中已弃用。使用#createFromXml(Resources,XmlPullParser parser,Theme) |
static ColorStateList |
createFromXml(Resources r, XmlPullParser parser, Resources.Theme theme) 使用给定的一组 |
int |
describeContents() 描述此Parcelable实例的封送表示中包含的特殊对象的种类。 |
int |
getChangingConfigurations() 返回此颜色状态列表可能更改的配置参数的掩码,要求重新创建该掩码。 |
int |
getColorForState(int[] stateSet, int defaultColor) 返回与给定的一组 |
int |
getDefaultColor() 返回此 |
boolean |
isOpaque() 指示此颜色状态列表是否不透明,这意味着从 |
boolean |
isStateful() 指示此颜色状态列表是否包含多个状态规范,并将根据状态更改颜色。 |
String |
toString() 返回对象的字符串表示形式。 |
static ColorStateList |
valueOf(int color) |
ColorStateList |
withAlpha(int alpha) 创建一个新的ColorStateList,它具有与此相同的状态和颜色,但每个颜色都具有指定的Alpha值(0-255)。 |
void |
writeToParcel(Parcel dest, int flags) 将此对象平铺到一个包裹中。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
android.os.Parcelable
|
|
应用于基本颜色的Alpha倍数。
必须是浮点值,例如“ 1.2 ”。
这也可能是对包含此类型值的资源(形式为“ @[package:]type:name ”)或主题属性(形式为“ ?[package:][type:]name ”)的 ?[package:][type:]name 。
这对应于全局属性资源符号 alpha 。
此状态的基础颜色。
必须是“ #rgb ”,“ #argb ”,“ #rrggbb ”或“ #aarrggbb ”形式的颜色值。
这也可能是对包含此类型值的资源(形式为“ @[package:]type:name ”)或主题属性(形式为“ ?[package:][type:]name ”)的 ?[package:][type:]name 。
这对应于全局属性资源符号 color 。
ColorStateList (int[][] states,
int[] colors)
创建一个ColorStateList,它返回从状态到颜色的指定映射。
| 参数(Parameters) | |
|---|---|
states |
int
|
colors |
int
|
ColorStateList createFromXml (Resources r, XmlPullParser parser)
此方法在API级别23中已弃用。
使用#createFromXml(资源,XmlPullParser解析器,主题)
从XML文档创建一个ColorStateList。
| 参数(Parameters) | |
|---|---|
r |
Resources: Resources against which the ColorStateList should be inflated. |
parser |
XmlPullParser: Parser for the XML document defining the ColorStateList. |
| 返回(Returns) | |
|---|---|
ColorStateList |
A new color state list. |
| 抛出异常(Throws) | |
|---|---|
XmlPullParserException |
|
IOException |
|
ColorStateList createFromXml (Resources r, XmlPullParser parser, Resources.Theme theme)
使用给定的一组 Resources和Resources.Theme从XML文档创建一个 Resources.Theme 。
| 参数(Parameters) | |
|---|---|
r |
Resources: Resources against which the ColorStateList should be inflated. |
parser |
XmlPullParser: Parser for the XML document defining the ColorStateList. |
theme |
Resources.Theme: Optional theme to apply to the color state list, may be null. |
| 返回(Returns) | |
|---|---|
ColorStateList |
A new color state list. |
| 抛出异常(Throws) | |
|---|---|
XmlPullParserException |
|
IOException |
|
int describeContents ()
描述此Parcelable实例的封送表示中包含的特殊对象的种类。 例如,如果对象将在writeToParcel(Parcel, int)的输出中包含writeToParcel(Parcel, int) ,则此方法的返回值必须包含CONTENTS_FILE_DESCRIPTOR位。
| 返回(Returns) | |
|---|---|
int |
a bitmask indicating the set of special object types marshaled by this Parcelable object instance. |
int getChangingConfigurations ()
返回此颜色状态列表可能更改的配置参数的掩码,要求重新创建该掩码。
| 返回(Returns) | |
|---|---|
int |
a mask of the changing configuration parameters, as defined by ActivityInfo |
也可以看看:
int getColorForState (int[] stateSet,
int defaultColor)
返回与给定的一组 View状态关联的颜色。
| 参数(Parameters) | |
|---|---|
stateSet |
int: an array of View states |
defaultColor |
int: the color to return if there's no matching state spec in this ColorStateList that matches the stateSet. |
| 返回(Returns) | |
|---|---|
int |
the color associated with that set of states in this ColorStateList. |
int getDefaultColor ()
返回此 ColorStateList的默认颜色。
| 返回(Returns) | |
|---|---|
int |
the default color in this ColorStateList. |
boolean isOpaque ()
指示此颜色状态列表是否不透明,这意味着从 getColorForState(int[], int)返回的每种颜色 getColorForState(int[], int)具有255的Alpha值。
| 返回(Returns) | |
|---|---|
boolean |
True if this color state list is opaque. |
boolean isStateful ()
指示此颜色状态列表是否包含多个状态规范,并将根据状态更改颜色。
| 返回(Returns) | |
|---|---|
boolean |
True if this color state list changes color based on state, false otherwise. |
也可以看看:
String toString ()
返回对象的字符串表示形式。 通常, toString方法会返回一个“文本地表示”该对象的字符串。 结果应该是一个简洁但内容丰富的表述,对于一个人来说很容易阅读。 建议所有子类重写此方法。
类Object的toString方法返回一个字符串,其中包含对象为实例的类的名称,符号字符“ @ ”以及对象的哈希代码的无符号十六进制表示形式。 换句话说,这个方法返回一个字符串,其值等于:
getClass().getName() + '@' + Integer.toHexString(hashCode())
| 返回(Returns) | |
|---|---|
String |
a string representation of the object. |
ColorStateList valueOf (int color)
| 参数(Parameters) | |
|---|---|
color |
int
|
| 返回(Returns) | |
|---|---|
ColorStateList |
A ColorStateList containing a single color. |
ColorStateList withAlpha (int alpha)
创建一个新的ColorStateList,它具有与此相同的状态和颜色,但每个颜色都具有指定的Alpha值(0-255)。
| 参数(Parameters) | |
|---|---|
alpha |
int: The new alpha channel value (0-255). |
| 返回(Returns) | |
|---|---|
ColorStateList |
A new color state list. |
void writeToParcel (Parcel dest, int flags)
将此对象平铺到一个包裹中。
| 参数(Parameters) | |
|---|---|
dest |
Parcel: The Parcel in which the object should be written. |
flags |
int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE. |