我尝试为RadioButtons设置自定义drawable,但我有一些问题。 (这些drawables只是搞砸了,设计师和我试图找出生成和使用从Paint Code导出的图标的最佳方法)。 这些是我的绘图:对于选定的状态
public class SelectedRadioButtonDrawable extends Drawable {
private RectF rectF;
public SelectedRadioButtonDrawable(Context context) {
final int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, context.getResources().getDisplayMetrics());
rectF = new RectF(0, 0, size, size);
}
@Override
public void draw(@NonNull Canvas canvas) {
StyleKitNameJava.drawSave(canvas, rectF, StyleKitNameJava.ResizingBehavior.AspectFill, true, false);
}
@Override
public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return PixelFormat.OPAQUE;
}
和默认状态:
public class RadioButtonDrawable extends Drawable {
private RectF rectF;
public RadioButtonDrawable(Context context) {
final int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, context.getResources().getDisplayMetrics());
rectF = new RectF(0, 0, size, size);
}
@Override
public void draw(@NonNull Canvas canvas) {
StyleKitNameJava.drawSave(canvas, rectF, StyleKitNameJava.ResizingBehavior.Center, false, false);
}
@Override
public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return PixelFormat.OPAQUE;
}
}
这是我的DrawableStateList
public class RadioButtonStateList {
private StateListDrawable stateListDrawable;
public StateListDrawable getStateListDrawable(Context context) {
stateListDrawable = new StateListDrawable();
Drawable radioButtonDrawable = new RadioButtonDrawable(context);
Drawable selectedRadioButtonDrawable = new SelectedRadioButtonDrawable(context);
stateListDrawable.addState(new int[]{android.R.attr.state_selected}, selectedRadioButtonDrawable);
stateListDrawable.addState(StateSet.WILD_CARD, radioButtonDrawable);
return stateListDrawable;
}
}
我在这里设置了单选按钮
for (VoteMonsterOption option : options) {
optionName = option.getOption();
optionId = option.getId();
RadioButton radioButton = new RadioButton(getContext());
radioButton.setPadding(padding, padding, padding, padding);
radioButton.setButtonDrawable(new RadioButtonStateList().getStateListDrawable(getContext()));
if (optionName != null) radioButton.setText(optionName);
if (optionId != null) radioButton.setTag(optionId);
voteRadioGroup.addView(radioButton);
}
我的第一个问题,RadioButton的文本与Drawable重叠,我找不到如何解决它。 我的第二个问题,州名单不起作用。如果我单击其中一个按钮,它的drawable不会更改为选定状态。