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

标签 java android radio-button

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

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

我想要的:可绘制左侧的空间
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;
}

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

最佳答案

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

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

关于java - 在单选按钮可绘制的左侧添加填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60566743/

相关文章:

java - Android XmlPullParser - 如何解析此 XML 示例文件?

java - ArrayList 和数组多排序

java - JUnit + Java + ErrorCollector 问题

android - Android 库可以接收推送通知吗?

android - 滚动时 BottomNavigationView 不隐藏

java - 什么是每行读取行的最快方法

android - 如何检查和验证给定的编辑文本是电话号码还是手机号码?

jquery - 我无法使用 Jquery 禁用多个字段

html - 如何处理在 HTML 表单中选择了哪个按钮?

javascript - 根据选择的单选按钮更改按钮链接