AndroidTips

地図の表示にはActivityをFragmentタグに記載する

1.Fragmentとは?

フラグメント (fragment) というのは、英語で「断片」とか「一部分」という意味です。 何の一部分かというと、フラグメントは「アクティビティの振る舞いの一部分」です。 画面の構成をFragment1とFragment2で構成し、横方向に並べて表示できると言うようなものです。
元々は、Fragmentは Android 3.0 (ハニコム "Honeycomb") ではじめてタブレットに適用されました。 スマホより大きな画面に対応する為に、考えられた表示法です。
従って、FragmentはAndroid 3.0以上が対象です。但し、Android Compatibility Packageを使うことにより、 Android2.3以前でも使用可能となります。
私はAndroid2.3.3を使用してアプリ開発をしていますので、以下の事例はAndroid2.3以前を対象としています。

2.レイアウトファイルの作成

従来のレイアウトファイル(main.xml)

com.google.android.maps.MapViewタグでmapの設定をする必要がありました。
apiキーは/res/values/strings.xmlファイルに記入するのが一般的でした。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <com.google.android.maps.MapView
            android:id="@+id/map"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:apiKey="@string/api_key"
            android:clickable="true"
            android:enabled="true" />
</LinearLayout>
		

Fragmentを使ったレイアウトファイル(main.xml)

android:id属性とandroid:name属性の指定が必要です。
apiキー設定はManufest.xmlで行います。

<?xml version="1.0" encoding="utf-8"?>
<fragment
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:name="com.google.android.gms.maps.SupportMapFragment"/>
		




Android Tipsに戻る