くごー先生の依頼は断れませんね。
なんか倒したくなったちゃいますね。はい。
今回はちょっと苦労しました。
■ 単純に PreferenceActivity の起動画面の背景だけ変えるなら、、、
* Window の背景を設定
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getWindow().setBackgroundDrawableResource(R.drawable.bg);
- addPreferencesFromResource(R.xml.pref);
- }
* ListView の背景を設定(その1)
# PreferenceActivity は ListActivity を extends してるんですよ。
# しってました?
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ListView lv = getListView();
- lv.setBackgroundResource(R.drawable.bg);
- addPreferencesFromResource(R.xml.pref);
- }
* ListView の背景を設定(その2)
main2.xml
- <?xml version="1.0" encoding="utf-8"?>
- <ListView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/bg"
- />
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main2);
- addPreferencesFromResource(R.xml.pref);
- }
ただし、この場合は問題があります!
次のような、root の PreferenceScreen の中に PreferenceScreen が入っている場合は、入れ子の PreferenceScreen の背景は変わってくれないのです!残念!
- <?xml version="1.0" encoding="utf-8"?>
- <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
- <PreferenceCategory android:title="Application Setting">
- <CheckBoxPreference
- android:key="checkbox_value"
- android:title="Setting1"
- android:summary="sample of checkbox setting" />
- <EditTextPreference
- android:key="edittext_value"
- android:title="Setting2"
- android:summary="sample of edittext setting"
- android:dialogTitle="Setting edittext"
- android:dialogMessage="please set values" />
- <PreferenceScreen
- android:title="PrefereceScreen2">
- <ListPreference
- android:key="list_value"
- android:title="Setting3"
- android:summary="sample of list setting"
- android:entries="@array/setting_items"
- android:entryValues="@array/setting_items_value" />
- <RingtonePreference
- android:key="ringtone_value"
- android:title="Setting4"
- android:summary="sample of ringtone setting"
- android:showDefault="true"
- android:showSilent="true"
- android:ringtoneType="ringtone" />
- </PreferenceScreen>
- </PreferenceCategory>
- </PreferenceScreen>
でも大丈夫。方法を見つけました。
PreferenceActivity が ListActivity を継承していることを思い出してください。
ListView のデフォルトの背景を変えてしまえばいいのです。
ということで、
■ 入れ子の PreferenceScreen も合わせて背景を変更
ListView のテーマを指定します。
style.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="CustomTheme.Black" parent="@android:style/Theme.Black">
- <item name="android:listViewStyle">@style/CustomListView</item>
- </style>
- <style name="CustomListView" parent="@android:style/Widget.ListView">
- <item name="android:background">@drawable/bg</item>
- <item name="android:cacheColorHint">@android:color/transparent</item>
- </style>
- </resources>
あとは、AndroidManifest.xml で指定するだけ
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- ... >
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- ...
- <activity android:name=".MainPreferenceActivity"
- android:label="@string/app_name"
- android:theme="@style/CustomTheme.Black"
- >
- </activity>
- </application>
- </manifest>
これでOK。
# Preference関係の style のパラメータは一行のなかのレイアウトなので使えませんでしたん。
ListView の背景をかえるときは CacheColorHint も一緒に指定しないとスクロールしたときなどにとっても残念なことになるので気をつけてね。
0 件のコメント:
コメントを投稿