Android - Spacing between CheckBox and text

前端 未结 29 2833
广开言路
广开言路 2020-11-27 09:24

Is there an easy way to add padding between the checkbox in a CheckBox control, and the associated text?

I cannot just add leading spaces, because my label is multi-

29条回答
  •  执念已碎
    2020-11-27 10:09

    You need to get the size of the image that you are using in order to add your padding to this size. On the Android internals, they get the drawable you specify on src and use its size afterwards. Since it's a private variable and there are no getters you cannot access to it. Also you cannot get the com.android.internal.R.styleable.CompoundButton and get the drawable from there.

    So you need to create your own styleable (i.e. custom_src) or you can add it directly in your implementation of the RadioButton:

    public class CustomRadioButton extends RadioButton {
    
        private Drawable mButtonDrawable = null;
    
        public CustomRadioButton(Context context) {
            this(context, null);
        }
    
        public CustomRadioButton(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public CustomRadioButton(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            mButtonDrawable = context.getResources().getDrawable(R.drawable.rbtn_green);
            setButtonDrawable(mButtonDrawable);
        }
    
        @Override
        public int getCompoundPaddingLeft() {
            if (Util.getAPILevel() <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                if (drawable != null) {
                    paddingLeft += drawable.getIntrinsicWidth();
                }
            }
            return paddingLeft;
        }
    }
    

提交回复
热议问题