<!--attrs.xml-->
<resources>
<declare-styleable name="View2">
<attr name="textColor" format="color"/>
<attr name="textSize" format="dimension"/>
</declare-styleable>
</resources>
<!--styles.xml-->
<resources>
<style name="View2">
<item name="textColor">#ffffffff</item>
<item name="textSize">22dip</item>
</style>
</resources>
<!--the View2 class defined-->
public View2(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.View2);
final int len =array.Length();
final int N =array.getIndexCount();//why N==0,because i initialize in styles.xml
}
<!--the callback method,like in Activity.onCreate(..)-->
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View2 v = new View2(this,null);
}
我在attrs.xml中声明了一个styable name="View2",然后我在styles.xml中初始化了两个attrs,然后在TypedArray中获取了Style,这是一个问题,为什么N==0,因为我初始化了这两个attrsin 样式.xml