2011-09-24 11 views
5

を表示されない私は、動的にテーブルの行を追加することに関する多くの記事を見てきましたが、私は私が欠けているのかわからないです。動的に追加されたテーブルの行が

私は次のことを実行すると、何も(脇アプリケーションのタイトルバーから)表示されません。

マイレイアウト:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
     android:id="@+id/table_view_test_main" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     > 
    <ScrollView 
      android:id="@+id/tvt_scroll" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      > 
     <RelativeLayout 
       android:id="@+id/tvt_scroll_relative" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
      <TableLayout 
        android:id="@+id/tvt_tableview" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        > 
      </TableLayout> 
     </RelativeLayout> 
    </ScrollView> 
</RelativeLayout> 

My活動:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.table_view); 

    TableLayout tableLayout = (TableLayout) findViewById(R.id.tvt_tableview); 

    TableRow tableRow = new TableRow(this); 
    tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 

    TextView column1 = new TextView(this); 
    column1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
    column1.setBackgroundColor(Color.YELLOW); 
    column1.setTextColor(Color.BLUE); 
    column1.setText("Col1 Value"); 
    tableRow.addView(column1); 

    TextView column2 = new TextView(this); 
    column2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
    column2.setBackgroundColor(Color.RED); 
    column2.setTextColor(Color.GREEN); 
    column2.setText("Col2 Value"); 
    tableRow.addView(column2); 

    tableLayout.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); 

    //tl.refreshDrawableState(); 
    //findViewById(R.id.tvt_scroll_relative).refreshDrawableState(); 
} 
+0

興味深いです。これはすべて私によく見えます(私は絶対にここに何か確信していません...)。 'TableLayout'の上の他のレイアウトに他の' TextView'を動的に追加することをお勧めします。あなたが何かを見せることができるかどうかを見てください。それがどのように機能するかお知らせください。 –

+0

tableRow.setLayoutParams();行を削除します。あなたがTableRowをインスタンス化した後、それは私が思うはずです。これがうまくいくかどうか私に教えてください。私はそれを解決策として掲示することができます。 –

+0

@ Yashwanth Kumar、それだけでは役に立たなかった。 ViewGroup.LayoutParamsをTableRow.LayoutParamsに変更するか、それらを完全に削除すると問題が解決されます。答えのための – CrackerJack9

答えて

11

変更の2つの参照

ViewGroup.LayoutParams 

TableRow.LayoutParams

0に

、それが動作するはずです。ウィジェットへのparams

追加レイアウトLayoutParamsが周囲のレイアウトコンテナの内部クラスであることを必要とします。

+0

おかげで、あなたは周囲のレイアウトコンテナの内部クラスを使用するための参照を持っているのですか? – CrackerJack9

+0

私は同じ問題を抱え、いくつかの経験の後でそれを発見しました。私は参考文献にも興味があります。 –

1

私は、これはあなたがTextView sのレイアウトのparamsを設定することとは何かを持っていると思います。これらの定義を削除すると、情報が表示されます。あなたはTableRowための公式ドキュメントを見てみた場合にも、あなたは見ることができます:

The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

+0

'TableRow.LayoutParams.span'を設定することができますので、' LayoutParam'sを設定する必要があります。 – CrackerJack9

関連する問題