简体   繁体   English

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

[英]android - How to add the view beside the radio button

I want to set the view beside the radio button in the radio group. 我想在收音机组中的单选按钮旁边设置视图。 I tried but i am not getting. 我试过但我没有得到。 please can anybody help me. 请任何人帮助我。

Source Code 源代码

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

在此输入图像描述

So what about that: 那怎么样:

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

On my phone it looks like this: 在我的手机上它看起来像这样:

在我的手机上它看起来像这样

--- some more words to my answer --- ---我的回答还有一些话 -

So well, I think the point is a misunderstanding with the LinearLayout. 好吧,我认为重点是对LinearLayout的误解。 That one does only support placing view Elements in a row. 那个只支持将视图元素放在一行中。 If you want to have View-Elements that are related to eachother you need the Relative Layout. 如果您想拥有与彼此相关的视图元素,则需要相对布局。 The RelativeLayout has a attributes like RelativeLayout有一个属性,如

android:layout_below="@+id/XXXXX"

which is a setting for a relative position to other view elements. 这是与其他视图元素的相对位置的设置。 So using this you can say that one element (like the TextView) is supposed to be below/above/leftTo/rightTo another view. 所以使用这个你可以说一个元素(比如TextView)应该低于/高于/ leftTo / right到另一个视图。 That was just the point. 那就是重点。 I hope that makes it easier to understand. 我希望这更容易理解。

Here you go. 干得好。 Its not an answer to the question. 它不是问题的答案。 But it can solve your issue. 但它可以解决您的问题。 YOu can set text of a radio button as follows... 你可以按如下方式设置单选按钮的文字......

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

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

You can design a layout in such a way as i have done. 您可以像我一样设计布局。 Here is the sample layout part : 以下是示例布局部分:

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

And in activity handle the oncheckChanged() event to perform the related functionality. 并且在activity中处理oncheckChanged()事件以执行相关功能。 but if you will use RadioGroup in layout, i think it won't be possible for you to achieve what you want. 但是如果你在布局中使用RadioGroup,我认为你不可能实现你想要的。

Note : In above sample where i used TextView you can use any view you want. 注意:在上面使用TextView的示例中,您可以使用任何所需的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM