【问题标题】:Radiobutton with text above button按钮上方带有文本的单选按钮
【发布时间】:2013-10-06 18:50:19
【问题描述】:

我是 android 新手,我需要在我的活动上添加单选按钮,但我需要将文本放在项目符号按钮的 to 上。

请帮忙。我发现了以下内容,虽然我不明白@drawable/main_selector 和@style/TabStyle 是什么。

Radiobutton with text on top

谁能给我一份101指南。

更新

我根据一些建议使用了以下方法,但没有用:

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
    android:text="something that is on top"
    android:id="@+id/toggle_tab_left"
    android:button="?android:attr/listChoiceIndicatorSingle"        
    style="@null"/>

<RadioButton 
    android:button="?android:attr/listChoiceIndicatorSingle"
    android:text="something that is on top"
    android:id="@+id/toggle_tab_right"
    style="@null"/>
</RadioGroup>

更新 2

我从 Warpzit 获得了我的解决方案,但是在我将问题标记为已回答之前,有人可以帮助我解决下面的对齐问题。 我将连续有 5 个单选按钮,其中一些将更长的文本分成 2 行。当文本适合屏幕、横向或平板电脑时,所有文本都应该在一行中:

更新 3

...根据屏幕大小,文本可以分成不同的行数。它不会总是标准的

【问题讨论】:

  • 这个问题被否决了,我不明白为什么。目标明确,问得很好。证明是它甚至很快就被正确回答了!!!

标签: android radio-button


【解决方案1】:

@style/TabStyle 只是一个应用的样式,你可以忽略它。 @drawable/main_selector 是根据情况切换的图形。您可以阅读有关选择器 here 的更多信息。

获取文本置顶的示例:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
    android:text="Text on top"
    android:button="@null"
    android:background="#f00"
    android:layout_weight="1"/>

<RadioButton 
    android:text="Text on top"
    android:button="@null"
    android:background="#0f0"
    android:layout_weight="1"/>
</RadioGroup>

将给出以下结果:

如果您希望文本出现在上方按钮,您可以使用以下 xml:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
    android:text="Text on top"
    android:button="@null"
    android:drawableBottom="@android:drawable/btn_radio"
    android:gravity="center"
    android:layout_weight="1"/>

<RadioButton
    android:text="Text on top"
    android:button="@null"
    android:drawableBottom="@android:drawable/btn_radio"
    android:gravity="center"
    android:layout_weight="1"/>
</RadioGroup>

这将给出以下结果:

【讨论】:

  • 我可以使用默认按钮,子弹,而不是@drawable/main_selector
  • @YiannisStavrianos 是的,但要指出的是,可绘制的按钮应该是“@null”,而您应该覆盖背景。
  • 请检查我的更新。文本仍然在右侧对齐。你有这个香草的例子吗?
  • 感谢它有效!根据更新 2,我现在有一个垂直对齐问题。您能否也就此提出建议。您的答案是正确的,所以无论如何我都会将其标记为正确。
  • @YiannisStavrianos 我不明白你的问题。您是否希望文本在太长时隐藏或者您要钓什么?
【解决方案2】:

为了补充Warpzit great answer,您应该在按钮上使用android:gravity="center_horizontal|bottom"android:layout_height="match_parent",以对齐它们。

另外,无需从 AOSP 复制单选按钮可绘制对象,请使用 ?android:attr/listChoiceIndicatorSingle

XML 布局

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radioGroup1"
    android:layout_width="400dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="20dp" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:button="@null"
        android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
        android:gravity="center_horizontal|bottom"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:button="@null"
        android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
        android:gravity="center_horizontal|bottom"
        android:text="RadioButton dsfsdfsdfsdfsdf" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:button="@null"
        android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
        android:gravity="center_horizontal|bottom"
        android:text="RadioButton fdsfsd fdsfsdf fsfsdfs" />

</RadioGroup>

【讨论】:

  • 如果我在 radiogroup 中添加单选按钮有问题怎么办
  • @IshaanPuniani 在将按钮添加到 RadioGroup 之前,在每个按钮上准备并设置 RadioGroup.LayoutParams
【解决方案3】:

使这种效果易于应用的一个好方法是使用样式。为此,请将此代码放在您的styles.xml 上,在resources 标记下。

<style name="RadioWithTextOnTop">
    <item name="android:button">@null</item>
    <item name="android:drawableBottom">?android:attr/listChoiceIndicatorSingle</item>
    <item name="android:gravity">center_horizontal|bottom</item>
</style>

然后,像这样将它应用到您的 RadioButton:

<RadioButton
    style="@style/RadioWithTextOnTop"
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="RadioButton" />

【讨论】:

    【解决方案4】:

    我最终使用的最简单的解决方案是:

    <RadioButton
        ...
        android:gravity="center_horizontal|bottom"
        android:drawableTop="?android:attr/listChoiceIndicatorSingle"
        android:button="@null"
        />
    

    它还使用了较新的动画图标listChoiceIndicatorSingle,这是默认样式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多