【问题标题】:Misaligned Radio button text未对齐的单选按钮文本
【发布时间】:2016-05-11 17:33:17
【问题描述】:

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

这是我添加按钮的组的布局定义:

<?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"/>

我的问题是我的文本与这样的单选按钮不对齐(我担心垂直对齐):

我确信这显然不是一个常见的情况,但是有人可以提示我应该从哪里找出我的问题吗?

编辑: 实际上我意识到如果我使用AppCompatRadioButton 会更好,因为我的目标是应用程序兼容性。现在这已更改(也有问题的来源),但没有区别。

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>

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

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

【问题讨论】:

  • 有趣的事实,对我来说它看起来像这样:i.stack.imgur.com/lwiEi.png(刚刚复制了您的代码并将其添加到空解决方案中;在 Lollipop 上运行)。您是否尝试过干净的项目 - 您是否也有这种行为?
  • 还有一点想法:你不需要android:layout_gravity="center_vertical"。而且您的问题看起来像是在某处 radioButton.setPadding(0,0,0,XXX); (或 android:paddingBottom="XXdp" 为您的 AppCompatRadioButton 即底部填充已设置在某处)或在代码后面的一些自定义主题。
  • 我将活动用作上下文。我正在几个设备 KitKat 和 Lollipop 上进行测试。我已经清理了这个项目一千次。我同意 abt layout_gravity,但它也没有伤害。我似乎没有找到任何填充或其他我错过与您分享的内容。
  • @BorisStrandjev 你能尝试编译并运行this test-project 并与我们分享,如果它还显示单选按钮未对齐?另外,只是一个额外的想法 - 检查你的 com.android.support:appcompat 支持库是“新鲜的”。
  • @KonstantinLoginov 感谢您为帮助我付出的巨大努力。我正要开始这个实验。不过,我将自己创建项目,因为我有项目在为 ADT 配置时遇到问题。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-04
    • 2017-01-28
    • 2011-09-05
    • 2011-03-10
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2011-03-06
    相关资源
    最近更新 更多