Android - Spacing between CheckBox and text

前端 未结 29 2686
广开言路
广开言路 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 09:55

    Simple solution, add this line in the CheckBox properties, replace 10dp with your desired spacing value

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

    If you are creating custom buttons, e.g. see change look of checkbox tutorial

    Then simply increase the width of btn_check_label_background.9.png by adding one or two more columns of transparent pixels in the center of the image; leave the 9-patch markers as they are.

    0 讨论(0)
  • 2020-11-27 09:58

    Checkbox image was overlapping when I used my own drawables from selector, I have solve this using below code :

    CheckBox cb = new CheckBox(mActivity);
    cb.setText("Hi");
    cb.setButtonDrawable(R.drawable.check_box_selector);
    cb.setChecked(true);
    cb.setPadding(cb.getPaddingLeft(), padding, padding, padding);

    Thanks to Alex Semeniuk

    0 讨论(0)
  • 2020-11-27 10:00

    I just concluded on this:

    Override CheckBox and add this method if you have a custom drawable:

    @Override
    public int getCompoundPaddingLeft() {
    
        // Workarround for version codes < Jelly bean 4.2
        // The system does not apply the same padding. Explantion:
        // http://stackoverflow.com/questions/4037795/android-spacing-between-checkbox-and-text/4038195#4038195
    
        int compoundPaddingLeft = super.getCompoundPaddingLeft();
    
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Drawable drawable = getResources().getDrawable( YOUR CUSTOM DRAWABLE );
            return compoundPaddingLeft + (drawable != null ? drawable.getIntrinsicWidth() : 0);
        } else {
            return compoundPaddingLeft;
        }
    
    }
    

    or this if you use the system drawable:

    @Override
    public int getCompoundPaddingLeft() {
    
        // Workarround for version codes < Jelly bean 4.2
        // The system does not apply the same padding. Explantion:
        // http://stackoverflow.com/questions/4037795/android-spacing-between-checkbox-and-text/4038195#4038195
    
        int compoundPaddingLeft = super.getCompoundPaddingLeft();
    
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            final float scale = this.getResources().getDisplayMetrics().density;
            return compoundPaddingLeft + (drawable != null ? (int)(10.0f * scale + 0.5f) : 0);
        } else {
            return compoundPaddingLeft;
        }
    
    }
    

    Thanks for the answer :)

    0 讨论(0)
  • 2020-11-27 10:01

    If you have custom image selector for checkbox or radiobutton you must set same button and background property such as this:

                <CheckBox
                    android:id="@+id/filter_checkbox_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:button="@drawable/selector_checkbox_filter"
                    android:background="@drawable/selector_checkbox_filter" />
    

    You can control size of checkbox or radio button padding with background property.

    0 讨论(0)
  • 2020-11-27 10:02

    What I did, is having a TextView and a CheckBox inside a (Relative)Layout. The TextView displays the text that I want the user to see, and the CheckBox doesn't have any text. That way, I can set the position / padding of the CheckBox wherever I want.

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