public ToggleButton(Context context, AttributeSet attrs) {
super(context, attrs);
@SuppressLint("Recycle")
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ToggleButton);
int indexCount = typedArray.getIndexCount();
for (int i = 0; i < indexCount; i++) {
/**
* 获取某个属性的ID值
*/
int index = typedArray.getIndex(i);
switch (index) {
case R.styleable.ToggleButton_curr_state:
currentState = typedArray.getBoolean(index, false);
case R.styleable.ToggleButton_my_background:
backgroundId = typedArray.getResourceId(index, -1);
if (backgroundId == -1) {
throw new RuntimeException("请设置背景图片");
}
slideBackgroundBitmap = BitmapFactory.decodeResource(getResources(), backgroundId);
break;
case R.styleable.ToggleButton_my_slideButton:
slideButtonId = typedArray.getResourceId(index, -1);
if (slideButtonId == -1) {
throw new RuntimeException("请设置按钮图片");
}
slideButton = BitmapFactory.decodeResource(getResources(), slideButtonId);
break;
}
}
}