在单选按钮可绘制的左侧添加填充

Add padding to the left of radio button drawable

我正在尝试在单选按钮的左侧添加填充。但是填充实际上是在可绘制对象和文本之间添加的。

我得到的结果:Space 在可绘制对象和文本之间

我想要的:Space在drawable的左边

我创建单选按钮的方法:

private RadioButton createRadioButton(String text) {
    RadioButton btn = new RadioButton(requireContext());

    btn.setId(View.generateViewId());
    btn.setText(text);
    btn.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    btn.setTextColor(answerButtonColor);
    btn.setButtonTintList(answerButtonColor);

    int pxHorizontal = Utils.convertDpToPx(requireContext(), 24);
    int pxVertical = Utils.convertDpToPx(requireContext(), 16);
    btn.setPadding(pxHorizontal, pxVertical, pxHorizontal, pxVertical);

    btn.setLayoutParams(new RadioGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
    ));

    return btn;
}

有没有办法在单选按钮的左侧添加填充?

RadioButton 是 CompundButton 的子类,所以我们在复合按钮上获取 drawable 并向其添加 inset。

val compoundButtonDrawable = CompoundButtonCompat.getButtonDrawable(radioButton1)
val insetDrawable = InsetDrawable(compoundButtonDrawable, 32, 0, 0, 0)
radioButton1.buttonDrawable = insetDrawable