【问题标题】:Place the text of the radio button inside of it将单选按钮的文本放在其中
【发布时间】:2020-02-13 19:52:05
【问题描述】:

如何在其中放置单选按钮的文本?

动机:

我创建了下面的菜单“硬编码”它,手动处理所有所需的情况,例如选择一个按钮时,必须取消选中所有其他按钮。

但是,它有很多代码,而这个功能正是单选组和单选按钮已经提供的功能,除了这个问题:单选按钮的文本放在按钮之外。

做了一些测试,我最终得到了以下代码:

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:button="@drawable/group_button"
  android:text="@string/char_A"
  />

属性 button 我设法改变了单选按钮的样式,并且连同 text 属性我已经实现了这个结果:

但我认为最简单的最后一个任务,我还没有完成。

我试图破解 Android 上 RadioButton 的源代码,以找到一些我可以重写的方法来替换此行为,但我最终进入了一个包含 13K 行代码的类,而且我认为我不能这么快就发现它是如何工作的。

【问题讨论】:

    标签: android radio-button custom-controls material-components-android android-chips


    【解决方案1】:

    你可以使用ChipGroup来达到想要的效果:

    <com.google.android.material.chip.ChipGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:selectionRequired="true"
        app:checkedChip="@+id/default_chip"
        app:singleSelection="true">
        <com.google.android.material.chip.Chip
            android:id="@+id/default_chip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:checkedIconVisible="false"
            android:checkable="true"
            android:text="A"/>
        <com.google.android.material.chip.Chip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:checkedIconVisible="false"
            android:checkable="true"
            android:text="B"/>
        <com.google.android.material.chip.Chip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:checkedIconVisible="false"
            android:checkable="true"
            android:text="C"/>
    </com.google.android.material.chip.ChipGroup>
    

    另请参阅ChipGroup 的参考以及Chips 的材料设计页面

    【讨论】:

    • 我试过这个解决方案,但文字在圆圈后面
    • @jairhumberto - 你的意思是因为圆圈看不到文字吗?
    • 是的,如果我设置一个小的负边距,它可能会看到文本放在圆圈后面。
    • @jairhumberto - 因为我添加了我的结果的屏幕截图,我相信你明白听到它不起作用我有点惊讶。为了安全起见,我将我的代码与另一个小示例应用程序一起使用,并且我可以确认至少对于某些构建工具/库版本等的星座。文本被可绘制对象覆盖:(我会尝试想一些其他的方法来达到预期的效果并回复你。
    • 谢谢,不过不要太在意这个,因为如果它成为一个大问题,我会保留我的旧解决方案。
    【解决方案2】:

    只需将单选按钮背景设置为这些字母图像或文本,然后在单选按钮 onClick() 中处理这些事件。

                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="true"
                        android:textSize="17sp"
                        android:onClick="onRadioClick"
                        android:textAppearance="@style/RobotoTextViewStyle"
                        android:background="@color/amber"/>
    

    添加您的字母可绘制图像,而不是颜色。

    【讨论】:

    • 没有别的办法了吗?用字母创建图像有点难看。
    猜你喜欢
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    相关资源
    最近更新 更多