【问题标题】:How to center the text in customized Radio Buttons in Android如何在 Android 的自定义单选按钮中居中文本
【发布时间】:2021-01-02 09:30:11
【问题描述】:

我在 Android 中使用了一些自定义的 RadioButtons。不幸的是,文本无法在中心对齐,尽管我将重力和 textAlignment 属性设置为“中心”。您可以在我的屏幕截图中看到右侧的文字更多:

这里你可以看到代码: 约束布局中的单选按钮:

  <RadioButton
        android:id="@+id/r_Button_Small"
        android:layout_width="@dimen/_73sdp"
        android:layout_height="@dimen/_28sdp"
        android:layout_weight="1"
        android:textSize="@dimen/_12ssp"
        android:background="@drawable/background_selector"
        android:text="@string/small"
        android:textColor="@drawable/text_selector"
        android:gravity="center"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.322"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="@dimen/_50sdp"
        app:layout_constraintVertical_bias="0.584"
         />

背景文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_checked="true"
        android:drawable="@drawable/state_checked" />

    <item android:drawable="@drawable/state_unchecked" />

</selector>

状态文件“已检查”

<?xml version="1.0" encoding="UTF-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorBlue">
    </solid>
    <corners android:radius="@dimen/_25sdp"></corners>
</shape>

和“未选中状态”

<?xml version="1.0" encoding="UTF-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorTest">
    </solid>
    <corners android:radius="@dimen/_25sdp"></corners>
</shape>

如果您想知道“@dimen/_25sdp”是什么:它是来自https://github.com/intuit/sdp 的可缩放尺寸单位

你知道如何将文本对齐到中心吗?我感谢您的每一条评论,并感谢您的帮助。

【问题讨论】:

  • 查看属性android:drawablePadding=""。我认为这可能会有所帮助。
  • 观看this link 以获得完整解释。
  • 感谢 Suvajit 的回答。我必须为该属性指定哪个值?
  • 只需对其进行硬编码并检查适合您的内容。给他们sps。
  • 你可以做的另一件事有点混乱。你可以创建一个LinearLayout,它有两个孩子,一个是没有任何标签的RadioButton,另一个是TextView。现在TextView 可以在中间了。

标签: android radio-button text-alignment


【解决方案1】:

用中心改变你的重力以填充和删除文本对齐

android:gravity="fill"

【讨论】:

  • 感谢您的回答 Tushar。如果我使用您的解决方案,垂直对齐会不正确。然后文本不与看起来不好的 RadioButton 的圆圈对齐
猜你喜欢
  • 2020-11-24
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 2013-05-08
  • 2014-12-03
相关资源
最近更新 更多