【问题标题】:Android: How to vertically-align:top a Radio button to a multiline text?Android:如何垂直对齐:将单选按钮顶到多行文本?
【发布时间】:2014-07-08 15:40:10
【问题描述】:

如您所见,我有一个简单的 RadioGroup 及其选项。

<RadioGroup
    android:id="@+id/radio_report_problems"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="20dp" >

    <RadioButton
        android:id="@+id/radiooption_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/report_problem_rd_opt_1" />

    <RadioButton
        android:id="@+id/radiooption_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/report_problem_rd_opt_2" />

    .....

</RadioGroup>

我的问题是项目符号位于文本的中间,而不是从第一行

不使用 tableView 或 linearLayout,有没有一种简单的方法可以使用 RadioGroup only 来实现?

谢谢。

【问题讨论】:

    标签: android android-radiogroup


    【解决方案1】:

    我用过

    android:gravity="top"

    它可以工作

    看到这个: enter image description here

    【讨论】:

      【解决方案2】:

      您可以扩展 RadioButton 并覆盖 onDraw 方法以绘制与文本顶部对齐的按钮。绘制该按钮的代码实际上是在 CompoundButton 内部(RadioButton 从那里扩展),所以这应该是你如何绘制按钮的起点。 Source

      我个人认为按钮与中心对齐没有什么问题,但这不是重点。

      【讨论】:

      • 在 HTML 中默认垂直对齐到顶部。我来自客户端,它对我来说似乎更干净。在Android 中,这样的列表定位是否很常见?感谢您的回答。难道没有比这更简单的方法了吗?
      • 这就是 Android 的 RadioButton(和 CompoundButton)的行为方式,我怀疑除了实际开发人员之外的任何人都知道为什么会这样,或者为什么无法以不同方式对齐按钮。我经常制作仅对现有视图进行微小修改的自定义视图,不幸的是我想不出更简单的方法来获得您想要的效果。
      • onDraw 方法是protected。这意味着您不能在扩展 RadioButton 的类中覆盖。
      • @AlamKanak 你绝对可以覆盖onDraw。 (就此而言,当您创建子类时,您绝对可以覆盖超类的任何protected 方法。)
      【解决方案3】:

      设置重力顶部 |单选按钮的底部

      radioButton.setGravity(Gravity.TOP | Gravity.BOTTOM);
      

      【讨论】:

        猜你喜欢
        • 2018-01-16
        • 2020-07-23
        • 1970-01-01
        • 1970-01-01
        • 2013-07-25
        • 2015-06-13
        • 1970-01-01
        • 2013-03-07
        • 2019-01-28
        相关资源
        最近更新 更多