Android - Spacing between CheckBox and text

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

    In my case I solved this problem using this following CheckBox attribute in the XML:

    *

    android:paddingLeft="@dimen/activity_horizontal_margin"

    *

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

    Android 4.2 Jelly Bean (API 17) puts the text paddingLeft from the buttonDrawable (ints right edge). It also works for RTL mode.

    Before 4.2 paddingLeft was ignoring the buttonDrawable - it was taken from the left edge of the CompoundButton view.

    You can solve it via XML - set paddingLeft to buttonDrawable.width + requiredSpace on older androids. Set it to requiredSpace only on API 17 up. For example use dimension resources and override in values-v17 resource folder.

    The change was introduced via android.widget.CompoundButton.getCompoundPaddingLeft();

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

    This behavior appears to have changed in Jelly Bean. The paddingLeft trick adds additional padding, making the text look too far right. Any one else notice that?

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

    For space between the check mark and the text use:

    android:paddingLeft="10dp"
    

    But it becomes more than 10dp, because the check mark contains padding (about 5dp) around. If you want to remove padding, see How to remove padding around Android CheckBox:

    android:paddingLeft="-5dp"
    android:layout_marginStart="-5dp"
    android:layout_marginLeft="-5dp"
    // or android:translationX="-5dp" instead of layout_marginLeft
    
    0 讨论(0)
  • 2020-11-27 10:07
    <CheckBox
            android:paddingRight="12dip" />
    
    0 讨论(0)
  • 2020-11-27 10:08

    Use attribute android:drawableLeft instead of android:button. In order to set padding between drawable and text use android:drawablePadding. To position drawable use android:paddingLeft.

    <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableLeft="@drawable/check_selector"
            android:drawablePadding="-50dp"
            android:paddingLeft="40dp"
            />
    

    result

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