declare-styleable的使用
declare-styleable是一个用于在XML文件中定义自定义View属性的标签。它的使用以下:
public class CustomView extends View {
private String customAttr1;
private int customAttr2;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
customAttr1 = a.getString(R.styleable.CustomView_customAttr1);
customAttr2 = a.getInteger(R.styleable.CustomView_customAttr2, 0);
a.recycle();
}
}
使用declare-styleable可以方便地定义和使用自定义View的属性,使得自定义View的属性配置更加灵活和可扩大。
TOP