开发者

Black screen in inner preferenceScreen

开发者 https://www.devze.com 2023-01-06 06:49 出处:网络
My PreferenceActivity contains a nested PreferenceScreen in another PreferenceScree开发者_StackOverflown and I\'m applying a theme to my PrefenceActivity that changes the background color. However whe

My PreferenceActivity contains a nested PreferenceScreen in another PreferenceScree开发者_StackOverflown and I'm applying a theme to my PrefenceActivity that changes the background color. However when I open the nested PreferenceScreen I get a black background and I cannot see the options.

This happens with android 2.1 , but it does not happen with android 1.6. Any ideas on how this can be corrected?


I found a way to do it but it quite a hack.

This is my prefs.xml

<PreferenceCategory
    android:title="@string/hello">

    <CheckBoxPreference
        key="pref_update_key"
        android:title="@string/hello"
        android:summaryOn="@string/hello"
        android:summaryOff="@string/hello"
        android:persistent="true"
        android:defaultValue="false" />

</PreferenceCategory>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="pref_second_preferencescreen_key" android:title="@string/hello">
        <CheckBoxPreference
        key="pref_update_key"
        android:title="@string/hello"
        android:summaryOn="@string/hello"
        android:summaryOff="@string/hello"
        android:persistent="true"
        android:defaultValue="false" />
</PreferenceScreen>

And this is my code for the class that extends PreferenceActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.layout.prefs);
    getWindow().setBackgroundDrawableResource(R.drawable.background);

    PreferenceScreen b = (PreferenceScreen) findPreference("pref_second_preferencescreen_key");
    b.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            PreferenceScreen a = (PreferenceScreen) preference;
            a.getDialog().getWindow().setBackgroundDrawableResource(R.drawable.background);
            return false;
        }
    });
}


What worked for me: Simply set a list style:

<style name="Theme.Preferences" parent="android:Theme.Light" >
    <item name="android:listViewStyle">@style/lightListView</item>
</style>
<style name="lightListView">
    <item name="android:background">#ffffff<item>
</style>


Workaround:

1) Prepare 2 PreferenceScreen xml's instead of sub PreferenceScreen using;

2) Add secondary PreferenceScreen Activity to AndroidManifest.xml:

<activity android:name="com.example.PreferenceActivity2"
          android:label="Issue4611"
          android:theme="@android:style/Theme.Light">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

3) For show secondary PreferenceScreen use in your first PreferenceScreen:

<PreferenceScreen android:key="key1"
                  android:title="1 Item"
                  android:summary="">
    <intent android:action="android.intent.action.VIEW"
            android:targetPackage="com.example"
            android:targetClass="com.example.PreferenceActivity2"/>
</PreferenceScreen>

Example


Macarse's answer is perfect, I was searching for classic white background so I changed this line in his answer:

a.getDialog().getWindow().setBackgroundDrawableResource(R.drawable.background);

to:

a.getDialog().getWindow().setBackgroundDrawableResource(android.R.color.white);

and it works nicely.

0

精彩评论

暂无评论...
验证码 换一张
取 消