2012-08-09 60 views
4

我想在廣播組中的單選按鈕旁邊設置視圖。我試過,但我沒有得到。請任何人都可以幫助我。android - 如何在單選按鈕旁邊添加視圖

源代碼

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

enter image description here

+0

我認爲它不可能。但是如果你解釋爲什麼你需要這樣做,我們可以找出其他方法。 我認爲你想在這些文字瀏覽中顯示兩個日期。對? – 2012-08-09 12:53:28

+0

在這裏,我想使用daterange選項,如「從日期選擇器到DatePicker」這就是爲什麼 – naresh 2012-08-09 12:58:06

+0

基於這個評論,@RahmathullahMPulikkal的答案應該是最好的解決方案,而不是試圖在旁邊放置一個新的視圖 – Cameron 2012-08-09 13:15:31

回答

7

那麼那個:

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

---一些更多的話給我的回答---

好吧,我認爲這一點是對LinearLayout的誤解。那個只支持將視圖元素連續放置。 如果您想要具有與彼此相關的視圖元素,您需要相對佈局。 該RelativeLayout的具有像

android:layout_below="@+id/XXXXX" 

,其爲相對位置的其它視圖的元件的設定的屬性。 所以使用這個,你可以說一個元素(比如TextView)應該在下面/上方/左邊/右邊到另一個視圖。 這只是重點。 我希望這可以讓你更容易理解。

+0

+1 @托比亞斯帝國好的答案... – NagarjunaReddy 2012-08-09 13:05:40

+0

是的..好的一個! – 2012-08-09 13:09:32

+0

你可能擴大一點來解釋爲什麼這是對這個問題的答案嗎?我知道你可能看起來很明顯,但是解釋一些代碼來展示你到底做了什麼通常很有幫助。 – 2012-08-09 22:57:13

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> 

0

可以以這樣的方式設計的佈局,因爲我已經做了。下面是示例佈局部分:

<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可以使用任何你想要的視圖。

+0

我認爲Tobias Reich的帖子也可以工作,但我還沒有嘗試過。你可以嘗試兩種。 – 2012-08-09 13:15:15

相關問題