我正在尝试在单选按钮的左侧添加填充。但填充实际上是添加在可绘制对象和文本之间。
我创建单选按钮的方法:
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/