【问题标题】:RadioButtons with TextView in RadioGroupRadioGroup 中带有 TextView 的 RadioButtons
【发布时间】:2012-05-05 02:51:34
【问题描述】:

除了扩展和创建我的自定义 RadioButton 之外,是否可以在 RadioGroup 中的 RadioButton 下方添加 TextView?

【问题讨论】:

    标签: android textview radio-button radio-group


    【解决方案1】:

    是的,有可能。见下文:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <RadioGroup
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
    
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="btn1" />
    
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="btn2" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="sample text" />
    
        </RadioGroup>
    
    </LinearLayout>
    

    样本输出

    【讨论】:

    • 问题开始了,当我需要将 TextView 放在两个 RadioButtons 之间时 - 不可能将它放在 RadioGroup 下,因为选择将被禁用并且它开始做一些时髦的事情。
    • 首先我将 textview 放在两个单选按钮之间(好的),后来我移到了底部(好的)。它有效,我测试了它。
    • 能否请您在底部之间和底部显示文本屏幕?不能让它工作:(
    • 我的意思是,布局确实很好,但功能会失去精度 - 如果我检查第三个 RadioButton,它会做一些奇怪的事情......
    • 只需在两个 CheckBox 之间移动 TextView。这就是我所做的,它奏效了
    【解决方案2】:

    您可以从 XML 中删除 RadioGroup 并一一添加单选按钮。 看看这个:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
      <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioGroup1"
        android:text="RadioButton" />
    
      <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioButton1"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
      <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="RadioButton" />
    </RelativeLayout>
    

    【讨论】:

    • 是的,但是我不能像使用组一样使用 RadioButtons - 这涉及使用自己的 RadioButton 机制进行大量编码...
    【解决方案3】:

    我认为您可以使用另一个棘手的解决方案。在两个带有 padding 或 margin 的单选按钮之间创建一个间隙,然后将 textView 放在间隙上。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
         android:layout_height="fill_parent" >
    
    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="78dp" >
    
        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="RadioButton" 
            android:layout_marginBottom="100dp"/>
    
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" 
            android:layout_marginBottom="100dp"/>
    
        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />
    
    </RadioGroup>
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioGroup1"
        android:layout_centerHorizontal="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:layout_alignParentTop="true"
        android:layout_marginTop="200dp"/>
    
    </RelativeLayout>
    

    【讨论】:

    • 不,不工作,尝试删除 center_horizo​​ntal...使其与屏幕顶部对齐...
    猜你喜欢
    • 1970-01-01
    • 2012-05-12
    • 2011-07-03
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 2012-06-10
    • 2017-01-14
    相关资源
    最近更新 更多