【问题标题】:android - How to add the view beside the radio buttonandroid - 如何在单选按钮旁边添加视图
【发布时间】:2012-08-06 16:32:17
【问题描述】:

我想在单选组中的单选按钮旁边设置视图。我试过了,但我没有。请任何人帮助我。

源代码

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:background="#FFFFFF">    

    <RadioGroup
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:checkedButton="@+id/view_completehistroy_rb"
            android:id="@+id/timeframe_group">

           <!-- View complete history Radio Button -->
           <RadioButton
                android:text="Complete History"
                android:textSize="16sp"
                android:layout_marginLeft="5dp"
                android:id="@+id/view_completehistroy_rb"
                android:textColor="#000000"
                />

            <!-- View history using date range Radio Button -->
            <RadioButton
                android:text="From"
                android:textSize="16sp"
                android:layout_marginLeft="5dp"
                 android:textColor="#000000"
                android:id="@+id/view_daterange_history_rb"/>

        </RadioGroup>

    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
          <!--Layout for Time selection  -->
          <LinearLayout 
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:layout_marginLeft="5dp"
              android:layout_toRightOf="@id/view_daterange_history_rb">                      

               <TextView
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_marginLeft="5dip"
                 android:layout_marginRight="5dip"
                 android:text="test1" 
                  android:textColor="#000000"
                 android:textSize="16sp"/>  

                <TextView
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_marginLeft="5dip"
                 android:layout_marginRight="5dip"
                 android:text="test2" 
                  android:textColor="#000000"
                 android:textSize="16sp"/>                  
          </LinearLayout>
    </RelativeLayout>
</LinearLayout>

【问题讨论】:

  • 我认为这是不可能的。但是,如果您解释为什么需要放置它,我们可以找到其他方法。我认为您想在这些文本视图中显示两个日期。对吗?
  • 这里我想使用日期范围选项,例如“从日期选择器到日期选择器”,这就是为什么
  • 基于此评论,@RahmathullahMPulikkal 的回答应该是最好的解决方案,而不是尝试在其旁边放置一个新视图

标签: android radio-group


【解决方案1】:

那又怎样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:background="#FFFFFF">    

    <RadioGroup
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:checkedButton="@+id/view_completehistroy_rb"
            android:id="@+id/timeframe_group">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <RadioButton
                android:id="@+id/view_completehistroy_rb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="Complete History"
                android:textColor="#000000"
                android:textSize="16sp" />

            <RadioButton
                android:id="@+id/view_daterange_history_rb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/view_completehistroy_rb"
                android:layout_marginLeft="5dp"
                android:text="From"
                android:textColor="#000000"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/view_daterange_history_rb"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:layout_toRightOf="@+id/view_daterange_history_rb"
                android:text="test1"
                android:textColor="#000000"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/textView1"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:layout_toRightOf="@+id/textView1"
                android:text="test2"
                android:textColor="#000000"
                android:textSize="16sp" />

        </RelativeLayout>

        </RadioGroup>
</LinearLayout>

在我的手机上是这样的:

---我的答案还有一些话---

好吧,我认为重点是对 LinearLayout 的误解。那个只支持将视图元素放在一行中。 如果你想拥有彼此相关的视图元素,你需要相对布局。 RelativeLayout 具有类似的属性

android:layout_below="@+id/XXXXX"

这是相对于其他视图元素的位置设置。 所以使用这个你可以说一个元素(比如 TextView)应该在另一个视图的下方/上方/leftTo/rightTo 之下。 这就是重点。 我希望这样更容易理解。

【讨论】:

  • 您能否扩展一点来解释为什么这是问题的答案?我知道这对您来说可能很明显,但解释一些代码以显示您的具体操作通常会有所帮助。
  • 此处对齐正常,但单选组的功能不起作用意味着这两个单选按钮一次被选中。一旦你明白了,你能检查一下吗?
  • 如果 RadioButtons 不是 RadioGroup 的第一个子项,RadioGroup 将无法正常工作。
  • 这不是一个正确的答案。此方法破坏了 RadioGroups 的用途...选择另一个单选按钮不会取消选择先前选择的单选按钮。
【解决方案2】:

给你。它不是问题的答案。但它可以解决你的问题。 您可以按如下方式设置单选按钮的文本...

RadioButton radBtn=(RadioButton)findViewById(R.id.view_daterange_history_rb);        
radBtn.setText("From 20/12/2000 to 21/12/2012");

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >

<RadioGroup
    android:id="@+id/timeframe_group"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:checkedButton="@+id/view_completehistroy_rb"
    android:orientation="vertical" >

    <!-- View complete history Radio Button -->

    <RadioButton
        android:id="@+id/view_completehistroy_rb"
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Complete History"
        android:textColor="#000000"
        android:textSize="16sp" />

    <!-- View history using date range Radio Button -->

    <RadioButton
        android:id="@+id/view_daterange_history_rb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="From"
        android:textColor="#000000"
        android:textSize="16sp" />
</RadioGroup>

【讨论】:

    【解决方案3】:

    您可以像我一样设计布局。这是示例布局部分:

        <RelativeLayout
                android:id="@+id/faxPannel"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/emailPannel" >
    
                <RadioButton
                    android:id="@+id/radioButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="30dp" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dip"
                    android:layout_marginRight="5dip"
                    android:layout_marginTop="10dp"
                    android:layout_toRightOf="@+id/radioButton"
                    android:layout_weight="1"
                    android:singleLine="true"
                    android:text="Label"
                    android:textColor="@color/Black"
                    android:textSize="16dp"
                    android:textStyle="bold" />
    
            </RelativeLayout>
    

    并在活动中处理 oncheckChanged() 事件以执行相关功能。但是如果您将在布局中使用 RadioGroup,我认为您将无法实现您想要的。

    注意:在上面我使用 TextView 的示例中,你可以使用任何你想要的视图。

    【讨论】:

    • 我认为 Tobias Reich 的帖子也可以,但我没有尝试过。两个都可以试试。
    猜你喜欢
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2013-10-01
    • 2020-01-29
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    相关资源
    最近更新 更多