How to center text in a switch

I set up the original radio object with a specific background and removing the button:

styles.xml:

<style name="echelle_relecture"> <item name="android:background">@drawable/btn_relecture_echelle</item> <item name="android:button">@null</item> <item name="android:layout_gravity">center_horizontal</item> </style> 

radio.xml:

 <RadioGroup android:layout_height="wrap_content" android:id="@+id/radioGroup1" android:layout_width="fill_parent" android:orientation="horizontal" android:layout_marginTop="10dp"> <RadioButton style="@style/echelle_relecture" android:text="@string/day" android:id="@+id/jour" android:checked="true"></RadioButton> <RadioButton style="@style/echelle_relecture" android:text="@string/hour" android:id="@+id/hour" android:button="@null"></RadioButton> <RadioButton style="@style/echelle_relecture" android:text="@string/min" android:id="@+id/mins" android:button="@null" android:enabled="false"></RadioButton> </RadioGroup> 

Despite the "center_horizontal" parameter, I can’t get the text centered inside the button. How can i do this?

+6
source share
4 answers

You should use gravity , not layout_gravity to centralize the text inside RadioButton .

+13
source

use this

 android:gravity="center" 
+6
source
 <RadioButton ... android:textAlignment="center" ... /> 
+1
source

Any android:paddingLeft="0dp" or android:paddingRight="0dp" should work to center the text

0
source

Source: https://habr.com/ru/post/894275/


All Articles