android attr.xml styles.xml
public RadioGroupEx(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray attributes = context.obtainStyledAttributes(attrs,
R.styleable.RadioGroupEx, R.attr.RadioGroupExStyle,
R.style.defRadioGroupExStyle);
//defRadioGroupExStyle 是自定义默认的风格,RadioGroupEx 是默然读取的风格属性, 这是从属性读功能<xml>中使用,
//RadioGroupExStyle 是我设定的风格属性读取点...这是在Style中的属性读取所以用attr.
int n = attributes.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = attributes.getIndex(i);
switch (attr) {
case R.styleable.RadioGroupEx_childPadding: //这里要用 styleable.属性组_组中属性.
childPadding = (int) attributes.getDimension(
R.styleable.RadioGroupEx_childPadding, 3);
break;
default:
break;
}
}
attributes.recycle();// 必须回收
init();
}
attr.xml
<declare-styleable name="RadioGroupEx">
<attr name="RadioGroupExStyle" format="reference" />
<attr name="childPadding" format="dimension" />
</declare-styleable>
styles.xml
<style name="defRadioGroupExStyle"> //这个是上面代码设定的默认风格
<item name="childPadding">12dp</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="RadioGroupExStyle">@style/defRadioGroupExStyle2</item> //因为属性中有RadioGroupExStyle是我设定的风格,可以在这设定自己的
</style>