2011-03-08 5 views
14

여러 개의 열이있는 Android 테이블을 만들고 싶습니다. 필자가 보았던 대부분의 예제는 2 개의 열이 있습니다. (필자는 Java와 Android에 익숙하지 않습니다.) 3-4 열이 필요하며 테이블에 동적으로 행을 추가 할 수 있어야합니다. 누구든지 샘플 코드를 제공 할 수 있습니까? (나는 이클립스에서 승리 7을 사용하고있다)여러 열이있는 Android에서 테이블을 만드는 방법은 무엇입니까?

답변

23

나는 당신이 데이터베이스의 테이블이 아니라 TableLayout 뷰에 대해 이야기하고 있다고 가정한다. ??

그렇다면 여기에 3 열 3 행이있는 XML 예제가 있습니다.

각 < TableRow> 요소는 테이블에 행을 만들고 요소 안의 각 뷰는 "열"을 만듭니다. 나는 TextViews을 사용했지만, 그들은 등 ImageViews, 글고 될 수

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id = "@+id/RHE" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:padding="5dp"> 

    <TableRow android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/runLabel" 
      android:text="R" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/hitLabel" 
      android:text="H" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/errorLabel" 
      android:text="E" 
      android:layout_height="wrap_content" 
      /> 
    </TableRow> 

    <TableRow android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/visitorRuns" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/visitorHits" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/visitorErrors" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
    </TableRow> 

    <TableRow android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/homeRuns" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/homeHits" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:id="@+id/homeErrors" 
      android:text="0" 
      android:layout_height="wrap_content" 
      /> 
    </TableRow> 
</TableLayout> 

동적으로 코드에서이를 변경하려면이 같은 거라고 :

// reference the table layout 
TableLayout tbl = (TableLayout)findViewById(R.id.RHE); 
// delcare a new row 
TableRow newRow = new TableRow(this); 
// add views to the row 
newRow.addView(new TextView(this)); // you would actually want to set properties on this before adding it 
// add the row to the table layout 
tbl.addView(newRow); 
+0

예, 내가 원하는을 테이블 레이아웃보기. 열의 위치가 잘못되었습니다. 기둥을 어떻게 배치합니까? 행을 어떻게 동적으로 추가 할 수 있습니까? – narayanpatra

+0

'제대로 배치되지 않았다'는 말을 정확히 모르겠습니다. TableLayout는 내부의 항목과 마찬가지로 속성에 따라 크기가 조정됩니다. 아마 xml을 게시하고보고 싶은 내용이 있으면 질문에 대답 할 수있는 능력이 더 많을 것입니다. – Peter

+0

나는 그대로 XML 파일의 코드를 사용했다. 처음 2 줄만 바꾸면됩니다. > narayanpatra

관련 문제