Android - Spacing between CheckBox and text

前端 未结 29 2650
广开言路
广开言路 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:08

    Setting minHeight and minWidth to 0dp was the cleanest and directest solution for me on Android 9 API 28:

    <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="0dp"
            android:minWidth="0dp" />
    
    0 讨论(0)
  • 2020-11-27 10:08

    <CheckBox android:drawablePadding="16dip" - The padding between the drawables and the text.

    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 10:10

    only you need to have one parameter in xml file

    android:paddingLeft="20dp"
    
    0 讨论(0)
  • 2020-11-27 10:11

    Instead of adjusting the text for Checkbox, I have done following thing and it worked for me for all the devices. 1) In XML, add checkbox and a textview to adjacent to one after another; keeping some distance. 2) Set checkbox text size to 0sp. 3) Add relative text to that textview next to the checkbox.

    0 讨论(0)
提交回复
热议问题