17

Androidで複数の列を持つテーブルを作成したい。私が見た例のほとんどは 2 列です。(私はJavaとAndroidが初めてです。)3〜4列が必要で、テーブルに動的に行を追加できるはずです。誰でもサンプルコードを提供できますか。(win 7でEclipseを使用しています)

4

1 に答える 1

25

データベース内のテーブルではなく、TableLayoutビューについて話していると思いますか?

もしそうなら、これは3つの列と3つの行を持つテーブルのXMLの例です。

各<TableRow>要素はテーブルに行を作成し、要素内の各ビューは「列」を作成します。私はTextViewsを使用しましたが、ImageViews、EditTextなどにすることができます。

<?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);
于 2011-03-08T06:04:21.277 に答える