2011-09-24 1 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> 

내 활동 :

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

변경 두 개의 참조

ViewGroup.LayoutParams 

TableRow.LayoutParams

이며 제대로 작동합니다.

위젯에 레이아웃 매개 변수를 추가하려면 LayoutParams이 주변 레이아웃 컨테이너의 내부 클래스 여야합니다.

+0

답변을 주셔서 감사합니다. 주변 레이아웃 컨테이너의 내부 클래스 사용에 대한 참조가 있습니까? – CrackerJack9

+0

나는 같은 문제를 경험하고 경험 한 후에 그것을 발견했다. 나는 약간의 참고 문헌에도 관심이있을 것이다. –

1

이것은 레이아웃 매개 변수를 TextView으로 설정하는 것과 관련이 있다고 생각합니다. 이러한 정의를 제거하면 정보가 나타납니다. 당신이 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'을 설정해야합니다. – CrackerJack9