2013-12-10 3 views
0

안드로이드의 테이블 레이아웃에 tablerows를 추가 할 때 약간의 문제가 있습니다. 문제가 어디 있는지 알아낼 수 없습니다Android에서 TableRow를 동적으로 추가 할 수 없습니다.

int z = 0; 
     for(String detail: details){ 
      TableRow tr = new TableRow(this); 
      tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

      //odd/even 
      if(z%2 == 0) 
       tr.setBackgroundColor(Color.parseColor("#F5F5F5")); 

      //set detail text 
      TextView detailText = new TextView(this); 
      RelativeLayout.LayoutParams dtlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);   
      detailText.setTextSize(16.0f); 
      detailText.setTextColor(Color.parseColor("#333333")); 
      detailText.setPadding(20, 10, 20, 10); 
      detailText.setLayoutParams(dtlp); 
      detailText.setText(detail); 
      tr.addView(detailText); 

      detailsTable.addView(tr); 

      ++z; 
     } 

:

여기 내 코드입니다. 세부 텍스트보기가 설정되어 있지만 tablerows가 표시되지 않습니다.

+0

모든 것이 위의 코드에서 정상이므로 LayoutParams로 작업해야합니다. ** detailsTable.addView (tr, new TableLayout.LayoutParams (..., ....)) ** – smkrn110

+0

Check * * TableLayout ** 높이 및 너비는 ** YOUR_LAYOUT.xml **에서 ** TableRow 너비를 MATCH_PARENT로 설정 했으므로 ** WRAP_CONTENT로 변경하십시오. – smkrn110

답변

0

a. "RelativeLayout.MATCH_PARENT"

또는

B에 dtlp의 "폭 매개 변수"를 변경해보십시오. Before For 루프 블록은 이와 같이 두 개의 레이아웃 매개 변수를 만듭니다.

루프에서
TableLayout.LayoutParams tlps=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT); 

TableRow.LayoutParams trps=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT); 

detailText.setLayoutParams(trps); 

함께

detailText.setLayoutParams(dtlp); 

를 교체하고 교체

detailsTable.addView(tr); 
이 도움이

detailsTable.addView(tr,tlps); 

희망

.

+0

이제 작동합니다. 고마워요! – Patricks

관련 문제