Android 如何将文本置于单选按钮的中心

Android 如何将文本置于单选按钮的中心,android,radio-button,Android,Radio Button,我已使用特定背景定制了原始radiobutton,并移除了该按钮: styles.xml: <style name="echelle_relecture"> <item name="android:background">@drawable/btn_relecture_echelle</item> <item name="android:button">@null</item> <item name="an

我已使用特定背景定制了原始radiobutton,并移除了该按钮:

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>
<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>

@可拉拔/btn_relecture_echelle
@空的
水平中心
radio.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>
<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>

尽管有“水平居中”参数,但我无法使文本在按钮内居中。我该怎么做呢?

用这个

android:gravity="center"

您应该使用
gravity
,而不是
layout\u gravity
将文本集中在
RadioButton
中设置
android:paddingLeft=“0dp”
android:paddingRight=“0dp”
都可以使文本居中
<RadioButton
    ...
    android:textAlignment="center"
    ... />

以您的风格添加代码

居中

虽然这个代码片段可以解决这个问题,但它确实有助于提高文章的质量。请记住,您将在将来为读者回答这个问题,而这些人可能不知道您的代码建议的原因。此代码是不言自明的,加油!