自定义 AppCompatRadioButton 与 Toggle Drawable 文本右侧

Custom AppCompatRadioButton with Toggle Drawable right of text

我正在尝试使用

将“Toggle Drawable”设置到 AppCompatRadioButton 文本的右侧
public class mRadioButton extends AppCompatRadioButton {

public mRadioButton(Context context) {
    super(context);
    init();
}
...

private void init() {
    setButtonDrawable(null);
    int padding = Constant.calculateDimen(getContext(), 20.0);
    setPadding(padding, padding, padding, padding);
    setGravity(Gravity.RIGHT);
    setLayoutParams(new FrameLayout.LayoutParams(-1, -1));

    int[] attrs = { android.R.attr.listChoiceIndicatorSingle };
    TypedArray ta = getContext().getTheme().obtainStyledAttributes(attrs);
    Drawable indicatorDrawable = ta.getDrawable(0);
    setCompoundDrawables(null, null, indicatorDrawable, null);
    ta.recycle();
}

}

但最终没有出现 Toggle Drawable :

如果设置评论 setButtonDrawable(null); 添加文本左侧切换:

并设置 setButtonDrawable(indicator); 出现 Toggle Drawable 左右文本:

这是怎么回事,出了什么问题,该怎么办?

谢谢@Napster 我改变了:

setCompoundDrawables(null, null, indicatorDrawable, null);

收件人:

setCompoundDrawablesWithIntrinsicBounds(null,null,indicatorDrawable,null);

但是为什么!?

然后我搜索发生了什么,发现了这个:

手动设置文字和图片的相对位置,常用方法如下:

setCompoundDrawables(left, top, right, bottom);


setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

意思是设置Drawable显示在文字上,左,右,下位置

但两者不同:

  1. setCompoundDrawables Paint drawable的宽高是drawable.setBound()设置宽高, 所以有 The Drawables 必须已经调用了 setBounds(Rect) 。 在使用长和宽之前必须使用Drawable.setBounds设置Drawable。

  2. setCompoundDrawablesWithIntrinsicBoundsDraw drawable的宽高为drawable固定宽高, 所以只需将 Drawables 的边界设置为它们的固有边界即可。