0

I'm try to set `Toggle Drawable' to right of AppCompatRadioButton' text with

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

}

But ultimately , no Toggle Drawable appears :

enter image description here

And if set comment setButtonDrawable(null); add Toggle left of text :

enter image description here

And set setButtonDrawable(indicator); appears Toggle Drawable both left an right of text:

enter image description here

What's going on , whats wrong and what to do?

Saeid
  • 2,261
  • 4
  • 27
  • 59

1 Answers1

0

With thanks @Napster i change :

setCompoundDrawables(null, null, indicatorDrawable, null);

To :

setCompoundDrawablesWithIntrinsicBounds(null,null,indicatorDrawable,null);

But Whay!?

Then i searched for what happened , that found this :

Manually set the relative position of text and pictures, commonly used methods are as follows:

setCompoundDrawables(left, top, right, bottom);


setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

The meaning is to set the Drawable display on the text, on the left, right, down position.

But the two are different:

  1. setCompoundDrawables Paint drawable's width and height is drawable.setBound () set the width and height, So there are The Drawables must already have had setBounds(Rect) called. You must use the Drawable.setBounds to set the Drawable before using the length and width.

  2. setCompoundDrawablesWithIntrinsicBoundsDraw drawable's width and height is drawable fixed width and height, So just have The Drawables' bounds will be set to their intrinsic bounds.

Saeid
  • 2,261
  • 4
  • 27
  • 59