有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在单选按钮drawable的左侧添加填充

我试图在单选按钮的左侧添加填充。但实际上,在可绘制和文本之间添加了填充

我得到的:可绘制和文本之间的空间
enter image description here

我想要的:drawable左侧的空间
enter image description here

我创建单选按钮的方法:

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;
}

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


共 (1) 个答案

  1. # 1 楼答案

    RadioButton是CompundButton的一个子类,因此我们可以在复合按钮上绘制,并向其添加插图

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