1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Android開発30日間マスターシリーズ - Day 5:UIレイアウトの基本 - LinearLayout、ConstraintLayout、RelativeLayoutの使い分け

Last updated at Posted at 2025-09-08

はじめに

スマホアプリを開発する上で、「どこに何を配置するか」というレイアウト設計は非常に重要です。この記事では、Android開発で使用する主要な3つのレイアウト方式について、実際のコード例と視覚的な解説を交えながら学んでいきます。

1. レイアウトの基本概念

レイアウトとは

レイアウトは、画面上のUI要素(ボタン、テキスト、画像など)をどのように配置するかを定義するXMLファイルです。建築の設計図のようなもので、アプリの見た目を決定する重要な要素です。

レイアウトファイルの基本構造

<?xml version="1.0" encoding="utf-8"?>
<!-- ここにレイアウト要素を記述 -->
<レイアウトタイプ xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <!-- UI要素(View)を配置 -->
    
</レイアウトタイプ>

共通属性の理解

<!-- すべてのViewで使用される重要な属性 -->
android:layout_width="wrap_content"    <!-- コンテンツに合わせる -->
android:layout_width="match_parent"    <!-- 親要素いっぱいに広げる -->
android:layout_width="0dp"             <!-- 制約に従って決定(ConstraintLayoutで使用)

android:layout_height="wrap_content"   <!-- 高さもwidthと同様 -->
android:layout_margin="16dp"           <!-- 外側の余白 -->
android:padding="8dp"                  <!-- 内側の余白 -->

2. LinearLayout(線形レイアウト)

概要と特徴

LinearLayoutは、子要素を一方向に一列に並べる最もシンプルなレイアウトです。

メリット:

  • 理解しやすい
  • シンプルなリスト形式のUIに最適
  • 重み(weight)を使った柔軟なサイズ調整

デメリット:

  • 複雑なUIではネストが深くなりがち
  • パフォーマンスの問題が発生しやすい

基本的な使い方

縦並び(vertical)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="タイトル"
        android:textSize="24sp"
        android:textStyle="bold" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:hint="名前を入力してください" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="送信" />

</LinearLayout>

横並び(horizontal)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp">

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/profile_icon"
        android:layout_marginEnd="12dp" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ユーザー名"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="最終ログイン: 2025年1月15日"
            android:textSize="12sp" />

    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="編集" />

</LinearLayout>

layout_weightの活用

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <!-- 画面幅の1/3を使用 -->
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="ボタン1" />

    <!-- 画面幅の2/3を使用 -->
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="ボタン2" />

</LinearLayout>

適用場面

  • ログインフォーム
  • 設定項目一覧
  • シンプルなリスト表示
  • ナビゲーションバー

3. RelativeLayout(相対レイアウト)

概要と特徴

RelativeLayoutは、他の要素や親レイアウトからの相対的な位置関係で要素を配置します。

メリット:

  • フラットな階層構造
  • 重なり合う要素の表現が得意

デメリット:

  • 複雑になると管理が困難
  • ConstraintLayoutに比べて制約が限定的
  • 現在は非推奨(新規開発では使用を避ける)

主要な位置指定属性

親レイアウト基準

android:layout_centerInParent="true"        <!-- 親の中央 -->
android:layout_centerHorizontal="true"      <!-- 水平方向の中央 -->
android:layout_centerVertical="true"        <!-- 垂直方向の中央 -->
android:layout_alignParentTop="true"        <!-- 親の上端 -->
android:layout_alignParentBottom="true"     <!-- 親の下端 -->
android:layout_alignParentStart="true"      <!-- 親の左端 -->
android:layout_alignParentEnd="true"        <!-- 親の右端 -->

他のView基準

android:layout_below="@id/other_view"        <!-- 他のViewの下 -->
android:layout_above="@id/other_view"        <!-- 他のViewの上 -->
android:layout_toStartOf="@id/other_view"    <!-- 他のViewの左 -->
android:layout_toEndOf="@id/other_view"      <!-- 他のViewの右 -->

実践例

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <!-- ヘッダー -->
    <TextView
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="アプリタイトル"
        android:textSize="24sp"
        android:textAlignment="center"
        android:background="#E3F2FD"
        android:padding="16dp" />

    <!-- 中央のコンテンツ -->
    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="メインコンテンツ"
        android:textSize="18sp" />

    <!-- フッター -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="キャンセル" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="OK" />

    </LinearLayout>

</RelativeLayout>

4. ConstraintLayout(制約レイアウト)【推奨】

概要と特徴

ConstraintLayoutは、**制約(Constraint)**を使って要素の位置とサイズを決定する、最も強力で推奨されるレイアウトです。

メリット:

  • フラットなビュー階層
  • 高いパフォーマンス
  • 柔軟で複雑なレイアウトに対応
  • ビジュアルエディタとの親和性が高い
  • レスポンシブデザインに最適

デメリット:

  • 学習コストがやや高い
  • 制約の概念の理解が必要

制約の基本概念

各Viewには4つの辺(上下左右)があり、それぞれに制約を設定します。

app:layout_constraintTop_toTopOf="parent"           <!-- 上辺を親の上辺に -->
app:layout_constraintBottom_toBottomOf="parent"     <!-- 下辺を親の下辺に -->
app:layout_constraintStart_toStartOf="parent"       <!-- 左辺を親の左辺に -->
app:layout_constraintEnd_toEndOf="parent"           <!-- 右辺を親の右辺に -->

基本的な使い方

中央配置

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/centerButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中央のボタン"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

チェーン(Chain)の活用

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="ボタン1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread"
        android:layout_marginEnd="8dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="ボタン2"
        app:layout_constraintStart_toEndOf="@+id/button1"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintTop_toTopOf="@+id/button1"
        android:layout_marginHorizontal="8dp" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="ボタン3"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button1"
        android:layout_marginStart="8dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

ガイドライン(Guideline)の使用

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 画面を30%と70%に分割するガイドライン -->
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.3" />

    <ImageView
        android:layout_width="200dp"
        android:layout_height="0dp"
        android:src="@drawable/sample_image"
        android:scaleType="centerCrop"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="詳細な説明文がここに入ります。"
        android:gravity="center"
        app:layout_constraintTop_toBottomOf="@+id/guideline"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:padding="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

実践的な例:プロフィール画面

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <!-- プロフィール画像 -->
    <ImageView
        android:id="@+id/profileImage"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/default_avatar"
        android:scaleType="centerCrop"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="32dp" />

    <!-- 名前 -->
    <TextView
        android:id="@+id/nameText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="山田太郎"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintTop_toTopOf="@+id/profileImage"
        app:layout_constraintStart_toEndOf="@+id/profileImage"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginStart="16dp" />

    <!-- メールアドレス -->
    <TextView
        android:id="@+id/emailText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="yamada@example.com"
        android:textSize="16sp"
        android:textColor="#666666"
        app:layout_constraintTop_toBottomOf="@+id/nameText"
        app:layout_constraintStart_toStartOf="@+id/nameText"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="4dp" />

    <!-- 編集ボタン -->
    <Button
        android:id="@+id/editButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="編集"
        app:layout_constraintTop_toBottomOf="@+id/profileImage"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="16dp" />

    <!-- 詳細情報 -->
    <TextView
        android:id="@+id/detailLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自己紹介"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintTop_toBottomOf="@+id/editButton"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="32dp" />

    <TextView
        android:id="@+id/detailText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="こんにちは!東京在住のソフトウェアエンジニアです。Android開発に興味があります。"
        android:textSize="16sp"
        android:lineSpacingMultiplier="1.2"
        app:layout_constraintTop_toBottomOf="@+id/detailLabel"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="8dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

5. レイアウトの比較と使い分け

性能比較

レイアウト パフォーマンス 学習コスト 柔軟性 推奨度
LinearLayout ⭐⭐⭐ ⭐⭐⭐⭐⭐ ⭐⭐ ⭐⭐⭐
RelativeLayout ⭐⭐⭐⭐ ⭐⭐⭐ ⭐⭐⭐⭐
ConstraintLayout ⭐⭐⭐⭐⭐ ⭐⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐

具体的な使い分けガイド

LinearLayoutを使う場面

✅ シンプルなフォーム
✅ 一方向の要素配列
✅ プロトタイピング
✅ ネストした構造の一部分

❌ 複雑なレイアウト
❌ 画面全体の設計

ConstraintLayoutを使う場面

✅ 画面全体のレイアウト設計
✅ 複雑な要素配置
✅ レスポンシブデザイン
✅ パフォーマンスが重要な場面
✅ ビジュアルエディタを使った設計

❌ 特になし(ほぼ万能)

RelativeLayoutを使う場面

❌ 新規開発では使用を避ける
⚠️  既存コードのメンテナンス時のみ

6. ベストプラクティスと設計Tips

1. パフォーマンスを意識した設計

<!-- ❌ 避けるべき深いネスト -->
<LinearLayout>
    <LinearLayout>
        <LinearLayout>
            <TextView />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<!-- ✅ フラットな構造 -->
<androidx.constraintlayout.widget.ConstraintLayout>
    <TextView />
</androidx.constraintlayout.widget.ConstraintLayout>

2. 保守性を考慮した命名

<!-- ❌ 曖昧な命名 -->
<Button android:id="@+id/button1" />

<!-- ✅ 意図が明確な命名 -->
<Button android:id="@+id/submitButton" />
<Button android:id="@+id/cancelButton" />

3. レスポンシブ対応

<!-- 様々な画面サイズに対応 -->
<androidx.constraintlayout.widget.ConstraintLayout>
    
    <!-- 幅は制約に従い、最小幅を指定 -->
    <TextView
        android:layout_width="0dp"
        android:minWidth="200dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
        
</androidx.constraintlayout.widget.ConstraintLayout>

4. Android Studioのビジュアルエディタの活用

  • Design画面でドラッグ&ドロップで直感的にレイアウト
  • Constraint設定は右クリック → "Constrain"メニューから
  • "Infer Constraints"で自動制約設定
  • "Convert to ConstraintLayout"で他のレイアウトから変換

7. よくあるエラーと対処法

制約不足エラー

エラー: ビューに十分な制約がありません

対処法: 各ビューに最低限の制約を設定
- 水平位置: start/end または left/right
- 垂直位置: top/bottom
<!-- ❌ 制約不足 -->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello" />

<!-- ✅ 適切な制約 -->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

循環依存エラー

エラー: View間で循環参照が発生

対処法: 制約の関係を見直し、一方向の依存関係にする

8. 次のステップ

学習の進め方

  1. 実際に手を動かす: Android Studioで様々なレイアウトを試す
  2. ビジュアルエディタを活用: Design画面で直感的に学習
  3. 既存アプリの分析: 人気アプリのレイアウトを参考にする
  4. Material Designの学習: Googleの設計ガイドラインを理解

推奨学習リソース

9. まとめ

重要なポイント

  1. ConstraintLayoutを主軸に: 新規開発では基本的にConstraintLayoutを使用
  2. 適材適所: シンプルな部分ではLinearLayoutも活用
  3. RelativeLayoutは避ける: 新規開発では使用しない
  4. フラットな構造: ネストを避けてパフォーマンスを向上
  5. レスポンシブ対応: 様々な画面サイズを考慮した設計

今回学んだこと

  • LinearLayout: シンプルな一方向配置
  • RelativeLayout: 相対位置による配置(非推奨)
  • ConstraintLayout: 制約ベースの柔軟なレイアウト(推奨)
  • 実践的な使い分け: 場面に応じた最適な選択
  • ベストプラクティス: 保守性とパフォーマンスを考慮した設計

次回予告

次回は「第6回:基本的なUI要素の実装 - Button、TextView、EditTextの活用法」をお届けします。今回学んだレイアウトの知識を基に、実際にユーザーが操作できるUI要素を詳しく学んでいきます。

この記事が役に立ったと思ったらいいね👍やストックをお願いします。


実践課題: 今回学んだ3つのレイアウトを使って、同じ見た目の画面を作成してみましょう。どのレイアウトが最も効率的かを比較してみてください。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?