未对齐单选按钮文本

时间:2016-02-02 18:48:03

标签: android android-layout radio-button layout-inflater

我的应用程序中有单选按钮,我通过代码动态添加。

以下是我将按钮添加到的组的layoutdefinition:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/answer_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:paddingLeft="15dp"
    android:layout_margin="20dp"
    android:orientation="vertical" >

</RadioGroup>

以下是我动态添加单选按钮的代码:

for (String value : values) {
   RadioButton radioButton = (RadioButton) ViewGroup.inflate(context,
            R.layout.radio_button_layout, null);
   radioButton.setText(value);
   answersRadioGroup.addView(radioButton);
}

radio_button_layout为:

<android.support.v7.widget.AppCompatRadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:paddingLeft="15dp"
    android:gravity="center_vertical"
    android:textColor="@color/font_color_gray"
    android:textSize="20sp"/>

我的问题是我的文字不像那样的单选按钮(我关注垂直对齐):

disaligned radio buttons

我确信这不是一个常见的案例,显然,有人可以暗示我应该在哪里寻找我的问题?

修改: 实际上我意识到如果我使用AppCompatRadioButton这样做会更好,因为我的目标是兼容app。现在这个被改变了(也是问题的来源),但没有区别。

EDIT2 以下是相关的风格,至少与我相符:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="windowNoTitle">true</item>
    <!-- We will be using the toolbar so no need to show ActionBar -->
    <item name="windowActionBar">false</item>
    <item name="android:radioButtonStyle">@style/MyRadioButtonStyle</item>
</style>

<!-- Styling the font of the radio buttons -->
<style name="MyRadioButtonStyle" parent="@android:style/Widget.Holo.Light.CompoundButton.RadioButton">
    <item name="android:button">@drawable/apptheme_btn_radio_holo_light</item>
</style>

正如人们可以看到我现在使用自定义按钮图标,但即使不使用自定义图标也存在问题。

EDIT3 现在它变得更有趣了。我确信不对中是由通货膨胀引起的。

我尝试修改RadioGroup布局如下:

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/answer_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:paddingLeft="15dp"
    android:layout_margin="20dp"
    android:orientation="vertical" >
    <android.support.v7.widget.AppCompatRadioButton
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:paddingLeft="15dp"
        android:gravity="center_vertical"
        android:textColor="@color/font_color_gray"
        android:text="Alabala"
        android:textSize="20sp"/>
</RadioGroup>

然后使用上面包含的代码附加实际的单选按钮。结果如下: enter image description here

正如您所看到的,布局中的单选按钮垂直对齐,但膨胀的按钮则没有。请问,有什么想法吗?

1 个答案:

答案 0 :(得分:0)

有两种不同的原因可能是造成此类问题的根本原因:

  • AppCompatRadioButton在布局
  • 后面的代码中设置paddingBottom
  • text属性中的不可见字符(确切地说这是问题背后的原因)