下面列出了android.widget.CheckBox#setBackgroundResource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void setupCheckBox(CheckBox checkBox) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
checkBox.setButtonDrawable(R.drawable.btn_checkbox_circle);
checkBox.setBackgroundResource(R.drawable.btn_checkbox_circle_background);
} else {
Context context = checkBox.getContext();
AppCompatDrawableManager dm = AppCompatDrawableManager.get();
StateListDrawable button = new StateListDrawable();
button.addState(new int[]{android.R.attr.state_checked},
dm.getDrawable(context, R.drawable.ic_checkbox_circle_checked));
button.addState(new int[]{},
dm.getDrawable(context, R.drawable.ic_checkbox_circle_unchecked));
ColorStateList buttonTint = new ColorStateList(new int[][]{ // states
new int[]{android.R.attr.state_checked},
new int[]{} // state_default
}, new int[]{ // colors
ThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated),
ThemeUtils.getThemeAttrColor(context, R.attr.colorControlNormal)
});
Drawable buttonCompat = DrawableCompat.wrap(button);
DrawableCompat.setTintList(buttonCompat, buttonTint);
checkBox.setButtonDrawable(buttonCompat);
ShapeDrawable background = new ShapeDrawable(new OvalShape());
int backgroundTint = ThemeUtils.getThemeAttrColor(context, android.R.attr.colorBackground);
Drawable backgroundCompat = DrawableCompat.wrap(background);
DrawableCompat.setTint(backgroundCompat, backgroundTint);
ViewCompatUtils.setBackground(checkBox, backgroundCompat);
}
}
private void initView() {
removeAllViews();
for (int i = 0; i < countNum; i++) {
CheckBox cb = new CheckBox(getContext());
LayoutParams layoutParams;
if (widthAndHeight == 0) {
layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
} else {
layoutParams = new LayoutParams((int) widthAndHeight, (int) widthAndHeight);
}
if (differentSize && countNum % 2 != 0) {
Log.e("xxx", layoutParams.width + "");
int index = i;
if (index > countNum / 2) {
index = countNum - 1 - index;
}
float scale = (index + 1) / (float) (countNum / 2 + 1);
layoutParams.width = (int) (layoutParams.width * scale);
layoutParams.height = layoutParams.width;
}
layoutParams.gravity = Gravity.CENTER_VERTICAL;
if (i != 0 && i != countNum - 1) {
layoutParams.leftMargin = (int) dividerWidth;
layoutParams.rightMargin = (int) dividerWidth;
} else if (i == 0) {
layoutParams.rightMargin = (int) dividerWidth;
} else if (i == countNum - 1) {
layoutParams.leftMargin = (int) dividerWidth;
}
addView(cb, layoutParams);
cb.setButtonDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
if (stateResId == -1) {
stateResId = R.drawable.book_review_rating_bar_selector;
}
cb.setBackgroundResource(stateResId);
if (i + 1 <= countSelected) {
cb.setChecked(true);
}
cb.setEnabled(canEdit);
cb.setOnClickListener(new MyClickListener(i));
}
}
private void initView() {
removeAllViews();
for (int i = 0; i < countNum; i++) {
CheckBox cb = new CheckBox(getContext());
LayoutParams layoutParams;
if (widthAndHeight == 0) {
layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
} else {
layoutParams = new LayoutParams((int) widthAndHeight, (int) widthAndHeight);
}
if (differentSize && countNum % 2 != 0) {
Log.e("xxx", layoutParams.width + "");
int index = i;
if (index > countNum / 2) {
index = countNum - 1 - index;
}
float scale = (index + 1) / (float) (countNum / 2 + 1);
layoutParams.width = (int) (layoutParams.width * scale);
layoutParams.height = layoutParams.width;
}
layoutParams.gravity = Gravity.CENTER_VERTICAL;
if (i != 0 && i != countNum - 1) {
layoutParams.leftMargin = (int) dividerWidth;
layoutParams.rightMargin = (int) dividerWidth;
} else if (i == 0) {
layoutParams.rightMargin = (int) dividerWidth;
} else if (i == countNum - 1) {
layoutParams.leftMargin = (int) dividerWidth;
}
addView(cb, layoutParams);
cb.setButtonDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
if (stateResId == -1) {
stateResId = R.drawable.book_review_rating_bar_selector;
}
cb.setBackgroundResource(stateResId);
if (i + 1 <= countSelected) {
cb.setChecked(true);
}
cb.setEnabled(canEdit);
cb.setOnClickListener(new MyClickListener(i));
}
}