我尝试为 RadioButtons 设置自定义可绘制对象,但我遇到了一些问题。(这些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 重叠,我不知道如何解决它。我的第二个问题,状态列表不起作用。如果我单击其中一个按钮,它的可绘制不会更改为选定状态。