2013-12-07 2 views
0

xml에 TableLayout이 있으며 프로그래밍 방식으로 행을 추가하려고합니다.TableLayout에 동적으로 추가 된 뷰가 표시되지 않습니다.

LinearLayout deckBuilder = (LinearLayout) getLayoutInflater().inflate(R.layout.deckbuilder, null); 

TableLayout deckGrid = new TableLayout(this.getApplicationContext()); 

    int rows = getResources().getInteger(R.integer.deckbuilder_grid_rows); 
    int columns = getResources().getInteger(R.integer.deckbuilder_grid_columns); 

    deckGrid.setWeightSum(rows); 

    for (int i = 0; i < rows; ++i) { 
     TableRow row = new TableRow(this.getApplicationContext()); 
     for (int j = 0; j < columns; ++j) { 
      ImageView iv = new ImageView(this.getApplicationContext()); 
      row.addView(iv); 
      iv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1)); 
      iv.setImageResource(R.drawable.my_image); 
     } 
     deckGrid.addView(row); 
     row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, 1)); 
    } 
    deckBuilder.addView(deckGrid); 
    deckGrid.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

문제는 내가 행에 추가 된 뷰 자체가 하나 표시되지 않는, 행을 표시되지 않는 것입니다 :

내 코드입니다.

행 3 열 : 5. 여기에

가 deckbuilder에 대한 XML입니다은 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/deckbuilder_background" 
    android:orientation="vertical" > 
</LinearLayout> 
+0

안녕하세요, 먼저 UI 구성 요소를 만들 때 applicationCOntext를 사용하면 안됩니다. 두 번째로 행과 열의 값을 출력 했습니까? 그렇다면 번호를 알려주세요. 우리에게 도움이 될 것입니다. 여기에 deckbuilder에 대한 xml을 추가하면 – Cata

답변

0

난 당신의 코드가 Activity를 확장하는 클래스 안에 가정합니다.

그래서 당신은 LinearLayout을 보여주기 위해 마지막 행 다음에

setContentView(deckBuilder); 

을 넣어해야합니다.

또한 Activity 안에 this.getApplicationContext() 대신 직접 this을 사용할 수 있습니다.

+0

setContentView (deckBuilder)가 클래스 시작 부분에서 완료됩니다. this.getApplicationContext() 대신 'this'를 사용하려고했지만 여전히 작동하지 않았습니다. –

+0

더 많은 코드를 게시 해보십시오. 내 장치에서 작동 ... – xonya

관련 문제