2012-06-06 42 views
0

我想將TableRow分成4個相等的部分。第一個是TextView,最後三個是RadioGroups。我已經成功將TableRow分成了這些相同的部分。但是,每行都是動態構建的,並且每個RadioGroup可能最多有5個RadioButtons。當建立一個TableRow與5 RadioButtons在每個RadioGroup所有的顯示和功能,但RadioButtons一起擠壓。有沒有辦法讓RadioGroup發生這種情況時有多條線路,但仍然保留在相同的TableRow的同一部分?基本上如果TextView在給定適當的參數時會變成多行的?對於這種情況Android - RadioButtons在RadioGroup中解決 - 格式問題

Java代碼:

RadioButton rb1, rb2, rb3, rb4, rb5; 
RadioGroup rg; 
TextView tv; 
TableLayout tl = (TableLayout) this.findviewById(R.string.tl); 
LayoutInflater inflater = getLayoutInflater(); 

//set up TableRow and TextView 
TableRow tr = (TableRow)inflater.inflate(R.layout.tablerow3q, tl, false); 
tr.setBackgroundColor(Color.LTGRAY); 
tv = (TextView)tr.findViewById(R.id.tv); 
tv.setText("Some text"); 

//loop sets up 1 RadioGroup per increment, each with 5 RadioButtons 
for(int i = 0; i < 3; i++) { 
    rb1 = new RadioButton(this); 
    rb2 = new RadioButton(this); 
    rb3 = new RadioButton(this); 
    rb4 = new RadioButton(this); 
    rb5 = new RadioButton(this); 

    rb1.setText("test1"); 
    rb2.setText("test2"); 
    rb3.setText("test3"); 
    rb4.setText("test4"); 
    rb5.setText("test5"); 

    //without these layouts, only four buttons are displayed, the other is cut off 
    rb1.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f); 
    rb2.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f); 
    rb3.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f); 
    rb4.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f); 
    rb5.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f); 

    //add RadioButtons to correct RadioGroup based on loop increment 
    if(i == 0) { 
     rg1 = (RadioGroup)tr.findViewById(R.id.rg1); 
     rg1.addView(rb1); 
     rg1.addView(rb2); 
     rg1.addView(rb3); 
     rg1.addView(rb4); 
     rg1.addView(rb5); 
    } 
    if(i == 1) { 
     rg1 = (RadioGroup)tr.findViewById(R.id.rg2); 
     rg1.addView(rb1); 
     rg1.addView(rb2); 
     rg1.addView(rb3); 
     rg1.addView(rb4); 
     rg1.addView(rb5); 
    } 
    if(i == 2) { 
     rg1 = (RadioGroup)tr.findViewById(R.id.rg3); 
     rg1.addView(rb1); 
     rg1.addView(rb2); 
     rg1.addView(rb3); 
     rg1.addView(rb4); 
     rg1.addView(rb5); 
    } 
} 
tl.addView(tr); 

TableRow XML佈局正被膨脹:

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"> 
    <TextView 
     android:id="@+id/tv" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_margin="1dip" 
     android:layout_weight="0.25" 
     android:background="#ffffff"/> 
    <RadioGroup 
     android:id="@+id/rg1" 
     android:layout_width="0dip" 
     android:orientation="horizontal" 
     android:layout_margin="1dip" 
     android:layout_weight="0.25" 
     android:background="#ffffff"/> 
    <RadioGroup 
     android:id="@+id/rg2" 
     android:layout_width="0dip" 
     android:orientation="horizontal" 
     android:layout_margin="1dip" 
     android:layout_weight="0.25" 
     android:background="#ffffff"/> 
    <RadioGroup 
     android:id="@+id/rg3" 
     android:layout_width="0dip" 
     android:orientation="horizontal" 
     android:layout_margin="1dip" 
     android:layout_weight="0.25" 
     android:background="#ffffff"/> 
</TableRow> 

回答

2

RadioGroup中延伸的LinearLayout,其不包其內容。有用於處理問題,但一對夫婦的選擇:

  1. 複製爲RadioGroup類的源代碼並修改它來擴展不同類型的佈局,如TableLayoutRelativeLayout。您需要管理單選按鈕的排列方式。

  2. 動態地構建一個嵌套TableLayout並使用OnClickListener s到處理選擇/取消選擇各種單選按鈕。

+0

解決了一段時間後的問題,但建議頭號正是我是如何做到的。沒有想到其他選擇,有趣的想法。 – Tucker