android-如何在单选按钮旁边添加视图

android-如何在单选按钮旁边添加视图,android,radio-group,Android,Radio Group,我想在“单选”组中的单选按钮旁边设置视图。我试过了,但没有成功。谁能帮帮我吗 源代码 <?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:orien

我想在“单选”组中的单选按钮旁边设置视图。我试过了,但没有成功。谁能帮帮我吗

源代码

 <?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>

那么这又如何呢:

<?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>
这是相对于其他视图元素的相对位置的设置。 因此,使用这个,您可以说一个元素(如TextView)应该位于另一个视图的下方/上方/左侧到右侧。 这正是重点。
我希望这样更容易理解。

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

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



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

    <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的示例中,您可以使用任何您想要的视图。

我认为这是不可能的。但是如果你解释一下为什么要这样做,我们可以找到其他的方法。我想你应该在那些文本视图中显示两个日期。是吗?我想在这里使用daterange选项,比如“从日期选取者到日期选取者”,这就是为什么基于这一评论,@RahmathullahMPulikkal的答案应该是最好的解决方案,而不是试图在它旁边放置一个新的视图。我认为Tobias Reich的帖子也可以工作,但我没有这样做。你可以两个都试一下。你能不能稍微扩展一下,解释一下为什么这是问题的答案?我知道这对你们来说可能很明显,但解释一些代码来显示你们到底做了什么通常是有帮助的。这里对齐工作正常,但单选组的功能不正常意味着这两个单选按钮一次被选中。如果单选按钮不是RadioGroup的第一个子项,则RadioGroup无法正常工作。这不是正确答案。这种方法破坏了放射组的用途。。。选择“其他”单选按钮不会取消选择以前选择的单选按钮。
<?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>
    <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>