icon
icon

Androidアプリ開発のTableLayoutの使い方【初心者向け】

Androidアプリ開発におけるレイアウトのうち【TableLayout】の使い方について初心者向けに解説した記事です。パーツを格子状に並べるときに使用するレイアウト。HTMLのtableタグと似たように表示されます。

テックアカデミーマガジンは受講者数No.1のプログラミングスクール「テックアカデミー」が運営。初心者向けにプロが解説した記事を公開中。現役エンジニアの方はこちらをご覧ください。 ※ アンケートモニター提供元:GMOリサーチ株式会社 調査期間:2021年8月12日~8月16日  調査対象:2020年8月以降にプログラミングスクールを受講した18~80歳の男女1,000名  調査手法:インターネット調査

Androidアプリ開発では、アプリの画面を簡単に作成できるレイアウトが数多く用意されています。

今回はそのなかの1つである、TableLayout(テーブルレイアウト)を追加する方法と基本的な使い方をご紹介します。

 

本記事はTechAcademyのAndroidアプリ開発オンラインブートキャンプの内容をもとに解説しています。

 

大石ゆかり

田島メンター!!HTMLのtableタグみたいにAndroidアプリのパーツを並べる方法ってないですか〜?

田島悠介

TableLayoutっていうレイアウトを使うと、tableタグと同じようにパーツを設置できるよ。

大石ゆかり

あ、そうなんですか〜!?

 

TableLayoutとは

TableLayoutとは、パーツを格子状に並べるときに使用するレイアウトです。

HTMLのtableタグと似ているので、ホームページ作成をしたことがある人は使いやすいレイアウトかもしれません。

ただし、行をまたいでパーツを配置することはできませんのでご注意ください。

 

 

TableLayoutを配置する

今回は、TableLayoutを使用して次のような画面を作成してみます。

TableLayoutは、大枠である「TableLayout」とテーブルの行である「TableRow」でできています。

 

最初にTableLayoutを配置します。

TableLayoutはレイアウトエディタのパレットの「レイアウト」から配置できます。パレット内のTableLayoutを、プレビュー画面までドラッグしましょう。

 

次に、作成する画面には4行必要なので、TableRowを4回プレビュー画面までドラッグして配置します。(行の数によってドラッグする回数を変えてください)

配置をしたらアウトラインで、レイアウトが正しく配置されているか確認をしましょう。

 

 

[PR] Androidアプリの開発で挫折しない学習方法を動画で公開中

TableLayoutにパーツを配置する

TableLayoutにパーツを配置するには、レイアウトエディタのパレットから配置したいパーツを選択し、プレビュー画面へドラッグします。

横の位置は後ほど調整しますので、目的の行に対して順番にパーツを配置できていれば大丈夫です。

 

 

TableLayoutのコードを確認する

XMLタブでコードを確認すると、下記のようになっています。

<TableLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >
<TableRow
android:id=”@+id/tableRow1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” >
<Button
android:id=”@+id/button1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”7″ />
<Button
android:id=”@+id/button2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”8″ />
<Button
android:id=”@+id/button3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”9″ />
<Button
android:id=”@+id/button15″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”/” />
</TableRow>
<TableRow
android:id=”@+id/tableRow2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” >
<Button
android:id=”@+id/button4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”4″ />
<Button
android:id=”@+id/button5″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”5″ />
<Button
android:id=”@+id/button6″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”6″ />
<Button
android:id=”@+id/button14″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”*” />
</TableRow>
<TableRow
android:id=”@+id/tableRow3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” >
<Button
android:id=”@+id/button7″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”1″ />
<Button
android:id=”@+id/button8″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”2″ />
<Button
android:id=”@+id/button9″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”3″ />
<Button
android:id=”@+id/button12″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”-” />
</TableRow>
<TableRow
android:id=”@+id/tableRow4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” >
<Button
android:id=”@+id/button10″
android:layout_width=”wrap_content”
android:text=”0″ />
<Button
android:id=”@+id/button11″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”.” />
<Button
android:id=”@+id/button13″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”+” />
</TableRow>
</TableLayout>

 

 

コラム

コスパとタイパ、両方結果的に良くなる良くなる学び方とは?

「スクールは高いし時間も縛られて効率が悪い」と考える方は多いと思います。
もちろん、時間も費用もかかることは間違いありません。
ただ 結果的に無駄な学びにお金も時間もかける方がリスクが高いという考えもあります。

コスパ・タイパ最適化の参考として、 テックアカデミー卒業生がスクールを選んだ理由 をご紹介します。

  • ・困ったときに、質問や相談できる相手がいるため挫折しなかった
  • ・プロとして必要なスキルのみを深く学べたので無駄がなかった
  • ・副業案件の提供と納品までのサポートがあったので目的を達成できた

安価・短期間で広く浅く学んでも意味がありません。 本当に自分の目的が達成できるか、それが重要です。
自分にどのスキルや学び方が合っているか、どんな学習方法かなど、お気軽に 無料相談 に参加してみませんか?

カウンセラー・現役のプロへ、何でも気軽に無料相談可能。 30分か60分お好きな時間が選べて、かつ3回まで すべて無料で ご利用できます。
無理な勧誘は一切ない ので、お気軽にご参加ください。

今なら相談した方限定の割引・参加特典付き! 無料相談はこちら

TableLayout上のパーツの配置を調整する

「0」ボタンは2つのセルをまたいで配置されています。

複数のセルをまたぎたい場合は、「android:layout_span」を追加してください。

<Button
android:id=”@+id/button10″
android:layout_width=”wrap_content”
android:layout_span=”2″
android:text=”0″ />

 

実行すると、この画像のようにボタンが表示されます。

 

また、途中のセルを空白にしたい場合は、「android:layout_column」を追加し、列番号を指定します。

列番号は、一番左から「0,1,2,3…」となっています。

<TableRow
android:id=”@+id/tableRow4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” >
<Button
android:id=”@+id/button10″
android:layout_width=”wrap_content”
android:layout_span=”2″
android:text=”0″ />
<Button
android:id=”@+id/button13″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_column=”3″
android:text=”+” />
</TableRow>

 

さらに実行すると、この画像のようにボタンが表示されます。

これで完成です。

 

さらに他のレイアウトを試したい場合はFrameLayoutの使い方も合わせてご覧ください。

 

大石ゆかり

tableタグを同じように設置できますね!

田島悠介

そうだね。GridLayoutもそうだけど一定の間隔でパーツを並べる場面って結構あるから、そういう時に使うと良いね。

大石ゆかり

そうですね〜♪

 

[お知らせ]TechAcademyでは初心者でもオリジナルアプリが作れるAndroidアプリ開発講座(オンラインブートキャンプ)を開催しています。自分でアプリを公開してみたい場合はご参加ください。

初心者・未経験でもできる。まずはテックアカデミーに相談しよう

プログラミングを独学で学習していて、このように感じた経験はないでしょうか?

  • ・調べてもほしい情報が見つからない
  • ・独学のスキルが実際の業務で通用するのか不安
  • ・目標への学習プランがわからず、迷子になりそう

テックアカデミーでは、このような 学習に不安を抱えている方へ、マンツーマンで相談できる機会を無料で提供 しています。
30分間、オンラインでどんなことでも質問し放題です。

「受けてよかった」と感じていただけるよう カウンセラーやエンジニア・デザイナー があなたの相談に真摯に向き合います。

「自分に合っているか診断してほしい」
「漠然としているが話を聞いてみたい」

こんなささいな悩みでも大丈夫です。

無理な勧誘は一切ありません ので、まずはお気軽にご参加ください。
※体験用のカリキュラムも無料で配布いたします。(1週間限定)

今なら参加者限定の割引特典付き! 無料相談を予約する