【问题标题】:Android: radiobutton text not well aligned with numberAndroid:单选按钮文本与数字没有很好地对齐
【发布时间】:2021-11-24 16:05:28
【问题描述】:

我有一个令人难以置信的问题。我有 1 个带有 2 个单选按钮的单选按钮,我希望每个单选按钮的宽度都是 match_parent,文本在左侧对齐。但是我不知道为什么,第一个是右对齐的,你可以在图片上看到

这是代码

<RadioGroup
    android:id="@+id/rgPasswordSendMode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="24dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="24dp"
    android:layout_marginBottom="32dp"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="vertical"
    android:showDividers="middle|end"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView">
     <RadioButton
        android:id="@+id/phoneRadioButton"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="start"
        android:layoutDirection="rtl"
        android:textAlignment="textStart"
        android:text="******6043" />
     <RadioButton
        android:id="@+id/emailRadioButton"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="start"
        android:layoutDirection="rtl"
        android:textAlignment="textStart"
        android:text="test@test.com" />
</RadioGroup>

当我仅将 ******6043 更改为 ****** 或其他文本时,标签在左侧很好地对齐。 基于enter link description here,我添加了

android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorSingle"
android:background="?android:selectableItemBackground"

但没有任何改变。

为什么会这样以及如何解决?

【问题讨论】:

  • 你能用match_content作为宽度,wrap_content作为高度
  • 不会改变任何东西

标签: android radio-button alignment text-alignment


【解决方案1】:

要解决这个问题,您应该添加android:textDirection="ltr",它定义了从左到右的文本方向。 android:layoutDirection="rtl" 定义布局绘制的方向,以便将单选按钮绘制到文本的右侧。

您的布局应如下所示:

<RadioGroup
    android:id="@+id/rgPasswordSendMode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="24dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="24dp"
    android:layout_marginBottom="32dp"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="vertical"
    android:showDividers="middle|end"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <RadioButton
        android:id="@+id/phoneRadioButton"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layoutDirection="rtl"
        android:textDirection="ltr"
        android:text="******6043" />

    <RadioButton
        android:id="@+id/emailRadioButton"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layoutDirection="rtl"
        android:textDirection="ltr"
        android:text="test@test.com" />
</RadioGroup>

结果:

【讨论】:

  • 非常感谢。我花了很多时间来寻找解决方案。如果可以,我会吻你:)。你知道为什么没有 `android:textDirection="ltr"` 它可以处理文本而不是数字吗?
  • 我认为这似乎是 RadioButton 中的一个错误,在将宽度设置为 match_parent 和 android:textAlignment="textStart" 后它应该适用于字母和数字
猜你喜欢
  • 2017-01-15
  • 1970-01-01
  • 2017-01-28
  • 2011-05-12
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 2011-09-05
  • 2011-03-10
相关资源
最近更新 更多