Add margin between a RadioButton and its label in Android?

后端 未结 18 1477
春和景丽
春和景丽 2020-12-02 10:43

Is it possible to add a little bit of space between a RadioButton and the label while still using Android\'s built-in components? By default the text looks a little scrunche

相关标签:
18条回答
  • 2020-12-02 11:20

    Add margin between a radiobutton its label by paddingLeft:

    android:paddingLeft="10dip"
    

    Just set your custom padding.

    RadioButton's xml property.

    <RadioButton
        android:id="@+id/radHighest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/YourImageResource"
        android:drawablePadding="50dp"
        android:paddingLeft="10dip"
        android:text="@string/txt_my_text"
        android:textSize="12sp" />
    

    Done

    0 讨论(0)
  • 2020-12-02 11:23
    <RadioButton
                    android:id="@+id/rb1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:background="@null"
                    android:paddingLeft="20dp"
                    android:text="1"
                    android:textColor="@color/text2"
                    android:textSize="16sp"
                    android:textStyle="bold" />
    
    0 讨论(0)
  • 2020-12-02 11:27

    i tried several ways and finished with this one working correctly on both emulator and devices:

        <RadioButton
            android:background="@android:color/transparent"
            android:button="@null"
            android:drawableLeft="@drawable/your_own_selector"
            android:drawablePadding="@dimen/your_spacing" />
    
    • android:background needs to be transparent as it seems on api10 there is a background with intrinsic paddings (not tried with other apis but the solution works on others too)
    • android:button needs to be null as paddings will not work correctly otherwise
    • android:drawableLeft needs to be specified instead of android:button
    • android:drawablePadding is the space that will be between your drawable and your text
    0 讨论(0)
  • 2020-12-02 11:28

    Create a style in style.xml like this

    <style name="Button.Radio">
    
        <item name="android:paddingLeft">@dimen/spacing_large</item>
        <item name="android:textSize">16sp</item>
    </style>
    

    Put that style in radio button

     <RadioButton
                    android:id="@+id/rb_guest_busy"
                    android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:text="@string/guest_is_waiting"
                    android:textSize="@dimen/font_size_3x_medium"
                    android:drawablePadding="@dimen/spacing_large"
                    android:textColor="@color/color_text_heading_dark"
                    style="@style/Button.Radio"/>
    

    You can change any attribute same as button as it RadioButton indirectly inherits button.

    0 讨论(0)
  • 2020-12-02 11:29

    Use the following XML attributes. It worked for me

    For API <= 16 use

    android:paddingLeft="16dp"
    

    For API >= 17 use

    android:paddingStart="@16dp"
    

    Eg:

    <android.support.v7.widget.AppCompatRadioButton
            android:id="@+id/popularityRadioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="true"
            android:paddingEnd="@dimen/radio_button_text"
            android:paddingLeft="@dimen/radio_button_text"
            android:paddingRight="@dimen/radio_button_text"
            android:paddingStart="@dimen/radio_button_text"
            android:text="Popularity"
            android:textSize="@dimen/sort_dialog_text_size"
            android:theme="@style/AppTheme.RadioButton" />
    

    Further More: drawablePadding attribute doesn't work. It only works if you added a drawable in your radio button. For Eg:

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:button="@null"
        android:drawableEnd="@android:drawable/btn_radio"
        android:drawablePadding="56dp"
        android:drawableRight="@android:drawable/btn_radio"
        android:text="New RadioButton" />
    
    0 讨论(0)
  • 2020-12-02 11:30

    I came here looking for an answer and the simplest way (after some thinking) was add spacing at the beginning of the label itself like so

    <RadioGroup
         android:orientation="horizontal"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignRight="@+id/btnChangeMode"
         android:layout_marginTop="10dp"
         android:layout_marginBottom="10dp"
         android:layout_below="@+id/view3"
         android:gravity="center|left"
         android:id="@+id/ledRadioGroup">
    <RadioButton
             android:button="@drawable/custom_radio_button"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text=" On"
             android:layout_marginRight="6dp"
             android:id="@+id/ledon"
             android:textColor="@color/white" />
    <RadioButton
             android:button="@drawable/custom_radio_button"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text=" Off"
             android:layout_marginLeft="6dp"
             android:id="@+id/ledoff"
             android:textColor="@color/white" />
    
    0 讨论(0)
提交回复
热议问题