【Androidアプリ作成】TableLayoutの使い方

HTMLの<table>タグのようにAndroidでも組むことができます。
下記は私が作成したサンプルコードです。

うまく表示することができれば、
上段に1、2,3という数字が入り
下段にみかん、りんご、メロンが入ります。

<TableLayout>は<LinearLayout>を継承したクラスなので、
基本のプロパティは一緒です。

★タグの説明
<TableLayout>・・・HTMLでいう<table>のことです。
<TableRow>・・・HTMLでいう<tr>のことです。

え、<td>なるものはないんですか?
結論から申し上げますとありません。

その代わりに
<View>、<textView>、<Button>の要素を置くことで<td>を表現します。

サンプルコード:フルーツテーブル)

<TableLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TableRow

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="1"

android:layout_weight="1"

android:gravity="center">

</TextView>

<TextView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="2"

android:layout_weight="1"

android:gravity="center">

</TextView>

<TextView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="3"

android:layout_weight="1"

android:gravity="center">

</TextView>

</TableRow>

<TableRow

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="みかん"

android:layout_weight="1"

android:gravity="center">

</TextView>

<TextView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="りんご"

android:layout_weight="1"

android:gravity="center">

</TextView>

<TextView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="メロン"

android:layout_weight="1"

android:gravity="center">

</TextView>

</TableRow>

</TableLayout>

2011.7.19
神森 真昼

追伸:
全く関係のない話ですが、本日は一緒にアプリ開発を行なっている
ゲームプログラマーNECOの誕生日です。おめでとう!
・・・といっても特に何もプレゼントがないですが^^;

コメントを残す

メールアドレスが公開されることはありません。

CAPTCHA

*