2013-08-03 5 views
2

TableLayout을 사용하여 복잡한 격자를 만들려고합니다.다른 프로그램 내에서 TableRow를 추가 할 수 없습니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="0.15"> 


    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_green_light" 
      android:layout_weight="1"> 
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_green_light" 
      android:layout_weight="1"> 

      </TableRow> 
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_orange_light" 
      android:layout_weight="1"> 

      </TableRow> 

      </TableRow> 
     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"> 

       <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_orange_light" 
      android:layout_weight="1"> 

      </TableRow> 
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_green_light" 
      android:layout_weight="1"> 

      </TableRow> 


     </TableRow>   
    </TableLayout> 

    <TableLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
       android:background="@android:color/holo_green_light" 
      android:layout_weight="1"/> 
     </TableLayout> 

    <TableLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_orange_light" 
      android:layout_weight="1"> 

      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_orange_light" 
      android:layout_weight="1"/>     
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_red_light" 
      android:layout_weight="1"/> 
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_blue_light" 
      android:layout_weight="1"/> 
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_green_light" 
      android:layout_weight="1"/> 

     </TableRow> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_green_light" 
      android:layout_weight="1"> 

      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_green_light" 
      android:layout_weight="1"/> 
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_orange_light" 
      android:layout_weight="1"/>     
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_red_light" 
      android:layout_weight="1"/> 
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_blue_light" 
      android:layout_weight="1"/> 

     </TableRow> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_blue_light" 
      android:layout_weight="1"> 

      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_blue_light" 
      android:layout_weight="1"/> 
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_green_light" 
      android:layout_weight="1"/> 
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_orange_light" 
      android:layout_weight="1"/>     
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_red_light" 
      android:layout_weight="1"/> 

     </TableRow> 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_red_light" 
      android:layout_weight="1"> 

      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_red_light" 
      android:layout_weight="1"/> 
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_blue_light" 
      android:layout_weight="1"/> 
       <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_green_light" 
      android:layout_weight="1"/> 
      <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/holo_orange_light" 
      android:layout_weight="1"/>  

     </TableRow> 
     </TableLayout> 

    </FrameLayout> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.85"></LinearLayout> 

문제가 있다는 것입니다 :이 코드에 의해

enter image description here

:이 같은 XML (그리드 상자의 일부가 강조되지 않음)에 의해 그것을 만들 관리 이 그리드의 파트 중 하나를 프로그램 적으로 만들려고하면 다른 TableRow의 자식 인 TableRow가 아니라 TableLayout의 직접 자식 인 TableRow 만 보여줍니다.

TableLayout table1 = new TableLayout(this); 
//Add TableLayout to a FramLayout holding multiple TableLayouts 
frame.addView(table1); 
table1.setBackgroundResource(R.color.some_random_color_for_debugging); 


for(int num = 0; num<2; num++){ 
    //4x2 
    TableRow row = new TableRow(this); 
    TableLayout.LayoutParams par = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT, 1f); 
    row.setLayoutParams(par); 
    if(num==1){ 
     row.setBackgroundResource(R.color.some_random_color_for_debugging1); 
    } 
    table1.addView(row, par);  
    for(int num1 = 0; num1<2; num1++){ 
     //2x2 
     TableRow box = new TableRow(this); 
     TableRow.LayoutParams param = new TableRow.LayoutParams(); 
     box.setLayoutParams(param); 
     if(num1==1){ 
      box.setBackgroundResource(R.color.some_random_color_for_debugging2); 
     } 
     row.addView(box, par);  
    } 


} 

이 코드는 some_random_color_for_debugging의 배경색 절반 화면 some_random_color_for_debugging1을 만드는 :이 자바 코드를 사용합니다.

TableRow 행과 상자를 모두 표시하려면 어떻게해야합니까?

+0

코드를 작성하려는 부분이 명확하지 않습니다. . 또한, 초기 레이아웃이 처음부터 나쁘다. 너무 많은 '무게'가 성능에 영향을 미치고 사용 중이다. – Luksprog

+0

@Luksprog 무게를 사용하지 않고 이런 레이아웃을 만들 수 있다는 것을 알고 계십니까? 부모보기의 크기를 조절하는 것은 주어진 수의 상자로 나누기 전에 옵션입니다. – Magakahn

답변

1

TableRow는 TableLayout이 제대로 작동하려면 부모로 필요하다고 생각합니다. 질문을 TableLayout/TableRow로 작업하게하려면 모든 행에 새 TableLayout을 추가해야합니다. tableLayout/TableRows가 그냥 요리 된 LinearLayouts이기 때문에 아마도 당신이 제안하고있는 구조로 손으로 그것을 조정할 수 있습니다.

하지만 조언을 수락하면 TableLayout을 전혀 사용하지 말라고 강력히 권고합니다. 나는 당신의 것과 비슷한 일을하기 위해 마지막 앱에서 많은 것을 사용했고, 마침내 그들 모두를 FrameLayout과 좌표 계산으로 대체했습니다. 필자의 경험에 따르면 TableLayouts의 성능은 끔찍한 데, 특히 TextView를 내부에 넣으면 성능이 떨어집니다. FrameLayout 방식이 예상대로 날아 다니는 반면, 10x10 크기의 테이블은 내 타블렛에서 프로세서를 먹습니다. (ultra-slow tablelayout performance) 전에 게시 한 질문을 살펴보면 성능과 관련하여 불만을 제기하는 몇 가지 관련 질문이있는 것을 확인할 수 있습니다.

+0

TableLayout을 사용하려는 이유는 더 쉽게 채울 수 있다는 것입니다. 나는/multiple array/s를 사용하여 그것을 채울 생각이다. – Magakahn

+0

시도해라. .. 진지하게. 나의 겸허 한 의견으로 Tablelayout는 끔찍하게 부서진다. 레이아웃이 복잡하지 않은 경우 가벼운 FrameLayout 또는 RelativeLayout을 사용하여 배치하는 것이 매우 간단합니다. 거의 동일한 방식으로 채울 수 있습니다.보기를 추가 할 때 명시적인 LayoutParams 만 추가하면됩니다. – rupps

+0

나는 그것을 줄 것이다. 내가 사용하는 LinearLayouts에는 RelativeLayout에없는 몇 가지 속성이 있다고 생각합니다. (수직 및 수평 방향 설정, Gravity.Center)로드 속도가 느려 집니까? – Magakahn