看起来这是API的一个bug。我测试了4个属性:gravity
、text
、textSize
和textColor
。以下是我使用的代码:
private void processAndroidAttributes(final Context context, final AttributeSet attrs) {
ATTRS = new Integer[] {
android.R.attr.textSize, android.R.attr.text,
android.R.attr.textColor, android.R.attr.gravity };
Arrays.sort(ATTRS);
do {
printPermutation(ATTRS);
tryApplyingStyles(context, attrs, ATTRS);
ATTRS = nextPermutation(ATTRS);
} while ((ATTRS = nextPermutation(ATTRS)) != null);
}
方法processAndroidAttributes
尝试应用所有列出的属性,并检查它们是否被应用或使用了默认值。对于每次迭代,我打印数组ATTRS
的内容以及是否使用了实际值(用1
标记)或使用了默认值(用0
标记)。运行上述代码后,我得到以下输出结果(每次迭代都会打印几行):
[16842901,16842904,16842927,16843087]
1111
[16842901,16842904,16843087,16842927]
1110
[16842901,16842927,16842904,16843087]
1101
[16842901,16842927,16843087,16842904]
1101
[16842901,16843087,16842904,16842927]
1100
[16842901,16843087,16842927,16842904]
1100
[16842904,16842901,16842927,16843087]
1011
[16842904,16842901,16843087,16842927]
1010
[16842904,16842927,16842901,16843087]
1011
[16842904,16842927,16843087,16842901]
1011
[16842904,16843087,16842901,16842927]
1010
[16842904,16843087,16842927,16842901]
1010
[16842927,16842901,16842904,16843087]
1001
[16842927,16842901,16843087,16842904]
1001
[16842927,16842904,16842901,16843087]
1001
[16842927,16842904,16843087,16842901]
1001
[16842927,16843087,16842901,16842904]
1001
[16842927,16843087,16842904,16842901]
1001
[16843087,16842901,16842904,16842927]
1000
[16843087,16842901,16842927,16842904]
1000
[16843087,16842904,16842901,16842927]
1000
[16843087,16842904,16842927,16842901]
1000
[16843087,16842927,16842901,16842904]
1000
[16843087,16842927,16842904,16842901]
1000
正如您所见,这几乎就像是期望数组已排序,只有几个离群值。我认为这是Android API中的一个错误,因为obtainStyledAttributes
的文档没有指定期望任何属性ID的顺序。