Androidでは設定項目に使用可能なコンポーネントとして、以下が用意されています。
今回のサンプルは、上記をすべて使用しています。なお今回のサンプルは、Android SDKに付属するApiDemoを流用して改良しています。
以下は、今回のサンプルのXMLレイアウトです。
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<RingtonePreference
android:key="ringtone_preference"
android:title="@string/ringtone_preference_title"
android:summary="@string/ringtone_preference_summary"
android:ringtoneType="ringtone" />
<PreferenceCategory
android:title="@string/inline_preferences">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/checkbox_preference_title"
android:summary="@string/checkbox_preference_summary" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/dialog_based_preferences">
<EditTextPreference
android:key="edittext_preference"
android:title="@string/edittext_preference_title"
android:summary="@string/edittext_preference_summary"
android:dialogTitle="@string/edittext_preference_dialog_title" />
<ListPreference
android:key="list_preference"
android:title="@string/list_preference_title"
android:summary="@string/list_preference_summary"
android:entries="@array/list_preference_entries"
android:entryValues="@array/list_preference_values"
android:dialogTitle="@string/list_preference_dialog_title" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/launch_preferences">
<PreferenceScreen
android:key="screen_preference"
android:title="@string/screen_preference_title"
android:summary="@string/screen_preference_summary">
<CheckBoxPreference
android:key="checkbox_preference2"
android:title="@string/checkbox_preference_title"
android:summary="@string/checkbox_preference_summary" />
<PreferenceScreen
android:key="screen_preference2"
android:title="@string/screen_preference_title"
android:summary="@string/screen_preference_summary">
<CheckBoxPreference
android:key="checkbox_preference3"
android:title="@string/checkbox_preference_title"
android:summary="@string/checkbox_preference_summary" />
</PreferenceScreen>
</PreferenceScreen>
<PreferenceScreen
android:title="@string/intent_preference_title"
android:summary="@string/intent_preference_summary">
<intent
android:action="android.intent.action.VIEW"
android:data="http://www.atmarkit.co.jp" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/preference_attributes">
<CheckBoxPreference
android:key="parent_checkbox_preference"
android:title="@string/parent_preference_title"
android:summary="@string/parent_preference_summary" />
<CheckBoxPreference
android:key="child_checkbox_preference"
android:dependency="parent_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"
android:title="@string/child_preference_title"
android:summary="@string/child_preference_summary" />
</PreferenceCategory>
</PreferenceScreen>
通常のActivityもそうですが、PreferenceActivityもきれいなXMLの入れ子構造です。
AndroidはXMLとコードの両方でレイアウトを定義できますが、国際化や多くのデバイスへの対応が容易なのはXML、動的なレイアウトはコーディングという基準で使い分けるとよいと思います。
さて、Androidでは、レイアウトをXMLで定義するのはごく自然なこととして受け入れられていますが、ほかのプラットフォームではどうでしょうか。
XML以外の定義ファイルを用いてGUIをレイアウトするものは、上記以外にもたくさんあります(VC++、JavaFXなどなど)。筆者としては、「Androidほど自然にレイアウトをXMLで組み込んでいるプラットフォームは、ほかにない」と思っています。
以下は、上記XMLの実際の設定画面です。
以降では、ポイントを押さえながらソースコードを詳細に見ていきます。
Copyright © ITmedia, Inc. All Rights Reserved.