自定义样式属性始终默认设置
Custom styled attribute always set by default
我已经为颜色定义了自定义样式属性..
<declare-styleable name="CustomView">
<attr name="custom1" format="boolean" />
<attr name="custom2" format="reference|color" />
</declare-styleable>
当通过 obtainStyledAttributes 获取 xml 中定义的属性时,它总是 returns 大小为 1,即使 xml 中没有定义任何属性。
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0);
final int size = attributes.getIndexCount();
for(int i = 0; i < size; i++) {
final int attr = attributes.getIndex(i);
if (attr == R.styleable.CustomView_custom1) {
boolean b = attributes.getBoolean(attr, false);
}
else if (attr == R.styleable.CustomView_custom2) {
ColorStateList cs = attributes.getColorStateList(attr);
int color = cs.getDefaultColor();
}
}
此处大小始终为 1,即使 xml 中未定义任何属性也是如此。
由于该颜色获得一些随机值,不确定它从哪里获得一些值?
输出如下:
Test﹕ ----
Test﹕ CustomView= 2130772048
Test﹕ CustomView= 2130772049
Test﹕ ----
Test﹕ AttributeSet= 16842901 textSize
Test﹕ AttributeSet= 16842904 textColor
Test﹕ AttributeSet= 16842927 gravity
Test﹕ AttributeSet= 16842960 id
Test﹕ AttributeSet= 16842966 paddingLeft
Test﹕ AttributeSet= 16842968 paddingRight
Test﹕ AttributeSet= 16842996 layout_width
Test﹕ AttributeSet= 16842997 layout_height
Test﹕ AttributeSet= 16843000 layout_marginTop
Test﹕ AttributeSet= 16843101 singleLine
Test﹕ AttributeSet= 16843119 drawableLeft
Test﹕ AttributeSet= 16843121 drawablePadding
Test﹕ AttributeSet= 16843139 layout_toRightOf
Test﹕ AttributeSet= 2130772048 custom1
奇怪的问题。我只能建议您添加调试日志输出并尝试更改以更清楚地了解正在发生的事情。
这些语句为您的自定义视图转储属性 ID,并在布局 XML.
中为您的自定义视图实例转储属性 ID。
Log.i("Test", "----");
for (int id : R.styleable.CustomView) {
Log.i("Test", "CustomView= " + id);
}
Log.i("Test", "----");
int n = attrs.getAttributeCount();
for (int i = 0; i < n; i++) {
Log.i("Test", "AttributeSet= " + attrs.getAttributeNameResource(i) + " " + attrs.getAttributeName(i));
}
查看主题中是否设置了custom2
属性:
TypedValue val = new TypedValue();
boolean resolved = context.getTheme().resolveAttribute(R.attr.custom2, val, true);
if (resolved) {
Log.i("Test", "Custom2 in theme");
} else {
Log.i("Test", "Custom2 NOT in theme");
}
对我来说问题是这样的:
private fun init(attrs: AttributeSet?) {
post {
val attributes = context.theme.obtainStyledAttributes(
attrs,
R.styleable.CustomButton,
0, 0
)
.....
如果我在 post{} 中获取属性,它却没有获取。这有效:
private fun init(attrs: AttributeSet?) {
val attributes = context.theme.obtainStyledAttributes(
attrs,
R.styleable.CustomButton,
0, 0
)
post {
....
我已经为颜色定义了自定义样式属性..
<declare-styleable name="CustomView">
<attr name="custom1" format="boolean" />
<attr name="custom2" format="reference|color" />
</declare-styleable>
当通过 obtainStyledAttributes 获取 xml 中定义的属性时,它总是 returns 大小为 1,即使 xml 中没有定义任何属性。
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0);
final int size = attributes.getIndexCount();
for(int i = 0; i < size; i++) {
final int attr = attributes.getIndex(i);
if (attr == R.styleable.CustomView_custom1) {
boolean b = attributes.getBoolean(attr, false);
}
else if (attr == R.styleable.CustomView_custom2) {
ColorStateList cs = attributes.getColorStateList(attr);
int color = cs.getDefaultColor();
}
}
此处大小始终为 1,即使 xml 中未定义任何属性也是如此。 由于该颜色获得一些随机值,不确定它从哪里获得一些值?
输出如下:
Test﹕ ----
Test﹕ CustomView= 2130772048
Test﹕ CustomView= 2130772049
Test﹕ ----
Test﹕ AttributeSet= 16842901 textSize
Test﹕ AttributeSet= 16842904 textColor
Test﹕ AttributeSet= 16842927 gravity
Test﹕ AttributeSet= 16842960 id
Test﹕ AttributeSet= 16842966 paddingLeft
Test﹕ AttributeSet= 16842968 paddingRight
Test﹕ AttributeSet= 16842996 layout_width
Test﹕ AttributeSet= 16842997 layout_height
Test﹕ AttributeSet= 16843000 layout_marginTop
Test﹕ AttributeSet= 16843101 singleLine
Test﹕ AttributeSet= 16843119 drawableLeft
Test﹕ AttributeSet= 16843121 drawablePadding
Test﹕ AttributeSet= 16843139 layout_toRightOf
Test﹕ AttributeSet= 2130772048 custom1
奇怪的问题。我只能建议您添加调试日志输出并尝试更改以更清楚地了解正在发生的事情。
这些语句为您的自定义视图转储属性 ID,并在布局 XML.
中为您的自定义视图实例转储属性 ID。Log.i("Test", "----");
for (int id : R.styleable.CustomView) {
Log.i("Test", "CustomView= " + id);
}
Log.i("Test", "----");
int n = attrs.getAttributeCount();
for (int i = 0; i < n; i++) {
Log.i("Test", "AttributeSet= " + attrs.getAttributeNameResource(i) + " " + attrs.getAttributeName(i));
}
查看主题中是否设置了custom2
属性:
TypedValue val = new TypedValue();
boolean resolved = context.getTheme().resolveAttribute(R.attr.custom2, val, true);
if (resolved) {
Log.i("Test", "Custom2 in theme");
} else {
Log.i("Test", "Custom2 NOT in theme");
}
对我来说问题是这样的:
private fun init(attrs: AttributeSet?) {
post {
val attributes = context.theme.obtainStyledAttributes(
attrs,
R.styleable.CustomButton,
0, 0
)
.....
如果我在 post{} 中获取属性,它却没有获取。这有效:
private fun init(attrs: AttributeSet?) {
val attributes = context.theme.obtainStyledAttributes(
attrs,
R.styleable.CustomButton,
0, 0
)
post {
....