2012-01-29 53 views
1

我有一個複雜的表,所以我想使一行更靈活一點,添加一個按鈕或文本視圖,所以我有以下代碼將TableRow添加到表。我想以編程方式將LinearLayout設置爲TableRow,但是當我運行它時,該行不顯示。如果有人能指出問題所在,我會很感激。android-tablerrow(包含LinearLayout)到表

  TableRow tr = new TableRow(this); 
     tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     final CheckBox checkbox = new CheckBox(this); 
     checkbox.setPadding(10, 5, 0, 0); 
     checkbox.setTextSize(TypedValue.COMPLEX_UNIT_PX, 15); 
     checkbox.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       String item_clicked_on = (String) ((TextView) view).getText(); 
       Toast.makeText(getApplicationContext(), item_clicked_on, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     TextView tv = new TextView(this); 
     tv.setText(" " + count + ". " + baby.getName()); 
     tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 14); 
     tv.setPadding(10, 10, 0, 0); 
     tv.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       String item_clicked_on = (String) ((TextView) view).getText(); 
       Toast.makeText(getApplicationContext(), item_clicked_on, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     checkbox.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
     tv.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 

     LinearLayout linearLayout = new LinearLayout(tr.getContext()); 
     linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     linearLayout.setOrientation(LinearLayout.HORIZONTAL); 
     linearLayout.addView(checkbox, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
     linearLayout.addView(tv, new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
     tableRow.addView(linearLayout); 

     table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

我想使它看起來像這個XML。

 <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
     <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
      <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
      <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Choice 1" android:textColor="#FFFFFF"></TextView> 
     </LinearLayout> 
    </TableRow> 

回答

0

你是tablerow的增加線性佈局。

tableRow.addView(linearLayout); 

但您要添加錶行TR

table.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

我認爲你必須tablerow的添加到。一旦檢查。

+0

我不需要添加linearLayout到TableRow和TableRow到表嗎? – user826323 2012-01-29 08:39:26

+0

@ user826323只需檢查變量** tableRow **和** tr **。哪一個你必須添加到**表**中。仔細閱讀以上答案。 – 2012-01-30 05:43:04