PreferenceActivity に LinearLayout を追加する

今となっては, PreferenceFragment に変わられ deprecated となっているが簡単に設定画面が作れる.

ネットで調べると, 面倒なのが多かった気がしたので貼っておく.

settings.xml をレイアウトとして作る.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/main"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/admob">
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/admob"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>

読み込み側 PreferenceActivity を親にもつ子.

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);
    ...

これで, けりがつく.

Preference や PreferenceScreen を作らなくてよい.

ミソは id の記述と

       <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            />

PreferenceActivity は ListActivity を親に持つということ.

java.lang.Object
   +	android.content.Context
 	   +	android.content.ContextWrapper
 	 	   +	android.view.ContextThemeWrapper
 	 	 	   +	android.app.Activity
 	 	 	 	   +	android.app.ListActivity
 	 	 	 	 	   +	android.preference.PreferenceActivity

RelaytiveLayout は必須.

PreferenceActivity | Android Developers