Android - Spacing between CheckBox and text

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

    API 17 and above, you can use:

    android:paddingStart="24dp"

    API 16 and below, you can use:

    android:paddingLeft="24dp"

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

    I end up with this issue by changing the images. Just added extra transparent background in png files. This solution works excellent on all the APIs.

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

    I had the same problem with a Galaxy S3 mini (android 4.1.2) and I simply made my custom checkbox extend AppCompatCheckBox instead of CheckBox. Now it works perfectly.

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

    As you probably use a drawable selector for your android:button property you need to add android:constantSize="true" and/or specify a default drawable like this:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true">
      <item android:drawable="@drawable/check_on" android:state_checked="true"/>
      <item android:drawable="@drawable/check_off"/>
    </selector>
    

    After that you need to specify android:paddingLeft attribute in your checkbox xml.

    Cons:

    In the layout editor you will the text going under the checkbox with api 16 and below, in that case you can fix it by creating you custom checkbox class like suggested but for api level 16.

    Rationale:

    it is a bug as StateListDrawable#getIntrinsicWidth() call is used internally in CompoundButton but it may return < 0 value if there is no current state and no constant size is used.

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

    All you have to do to overcome this problem is to add android:singleLine="true" to the checkBox in your android xml layout:

    <CheckBox 
       android:id="@+id/your_check_box"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:singleLine="true"
       android:background="@android:color/transparent"
       android:text="@string/your_string"/>
    

    and nothing special will be added programmatically.

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

    I don't know guys, but I tested

    <CheckBox android:paddingLeft="8mm" and only moves the text to the right, not entire control.

    It suits me fine.

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