Add margin between a RadioButton and its label in Android?

后端 未结 18 1479
春和景丽
春和景丽 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:31

    The padding between the drawables and the text. It will be achieved by adding line below in xml file. android:drawablePadding="@dimen/10dp"

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

    For anyone reading this now, the accepted answer will lead to some layout problems on newer APIs causing too much padding.

    On API <= 16 you can set paddingLeft on the radio button to set the padding relative to the radio button's view bounds. Additionally, a patch nine background also changes the view bounds relative to the view.

    On API >= 17 the paddingLeft (or paddingStart) is in relation to the radio button drawable. Same applies to the about a patch nine. To better illustrate padding differences see the attached screenshot.

    If you dig through the code you will find a new method in API 17 called getHorizontalOffsetForDrawables. This method is called when calculating the left padding for a radio button(hence the additional space illustrated in the picture).

    TL;DR Just use paddingLeft if your minSdkVersion is >= 17. If you support API <= 16, you should have radio button style for the min SDK you are supporting and another style for API 17+.

    combine screenshots showing left padding differences between API versions

    0 讨论(0)
  • 2020-12-02 11:36
    final float scale = this.getResources().getDisplayMetrics().density;
    checkBox.setPadding(checkBox.getPaddingLeft() + (int)(10.0f * scale + 0.5f),
    
        checkBox.getPaddingTop(),
        checkBox.getPaddingRight(),
        checkBox.getPaddingBottom());
    
    0 讨论(0)
  • 2020-12-02 11:36

    I'm using different approach that I think should work on all API versions. Instead of applying padding I'm adding an empty view between to RadioButtons:

    <View
            android:layout_width="20dp"
            android:layout_height="1dp" />
    

    This should give you 20dp padding.

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

    I know it is an old question, but with this solution, I finally got peace of mind and forget about API level.

    Left side vertical RadioGroup with right side vertical text view.

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">
            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </RadioGroup>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="8dp"
                android:gravity="center_vertical"
                android:text="Radio 1"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="8dp"
                android:gravity="center_vertical"
                android:text="Radio 2"/>
        </LinearLayout>
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-02 11:39

    The "android:paddingLeft" only seems to work correctly under android 4.2.2

    i have tried almost all versions of android and it only works on the 4.2.2 version.

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