始终默认设置为自定义样式的属性

问题描述 投票:1回答:2

我已经定义了颜色的自定义样式属性。

<declare-styleable name="CustomView">
        <attr name="custom1" format="boolean" />
        <attr name="custom2" format="reference|color" />
</declare-styleable>

当通过gettainStyledAttributes获取在xml中定义的属性时,即使在xml中未定义任何属性,它也总是返回size为1。

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();
    }
}

即使在xml中未定义任何属性,此处的大小也始终为1。由于该颜色获得一些随机值,因此不确定从何处获得某种值?

输出如下:

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

我已经定义了颜色的自定义样式属性。.[[

android android-styles
2个回答
1
投票

奇怪的问题。我只能建议您添加调试日志输出并尝试进行更改,以使更多了解发生的情况。

这些语句在布局XML中转储您的自定义视图的属性ID,并在AttributeSet中转储您的自定义视图的实例的属性ID。


0
投票

对我来说问题是这样:

© www.soinside.com 2019 - 2024. All rights reserved.