开发者

Background from PreferenceActivity is not applied to sub-PreferenceScreen

I am testing my application on a Nexus One and i have some prob开发者_开发问答lems. My theme is Light and when an inner sub PreferenceScreen is displayed, the window background becomes black instead of keeping the PreferenceActivity's one.

<PreferenceScreen android:title="main preferences">
    ...
    <PreferenceScreen android:title="sub screen">
    </PreferenceScreen>
</PreferenceScreen>

What is the problem?

Wouter


Use this:

Create theme in style.xml file

<style name="Theme.SettingsBackground" parent="@android:style/Theme.NoTitleBar">
<item name="android:windowBackground">@android:color/black</item>
</style>

and then in manifest file use:

<activity android:name=".Settings" android:theme="@style/Theme.SettingsBackground"></activity>

Do this for all sub activities which you want.


To best understand what is happening here you can refer to this piece of code from the source code for the PreferenceScreen class:

 @Override
    protected void onClick() {
        if (getIntent() != null || getPreferenceCount() == 0) {
            return;
        }

        showDialog(null);
    }

    private void showDialog(Bundle state) {
        Context context = getContext();
        ListView listView = new ListView(context);
        bind(listView);

        // Set the title bar if title is available, else no title bar
        final CharSequence title = getTitle();
        Dialog dialog = mDialog = new Dialog(context, TextUtils.isEmpty(title)
                ? com.android.internal.R.style.Theme_NoTitleBar
                : com.android.internal.R.style.Theme);
        dialog.setContentView(listView);
        if (!TextUtils.isEmpty(title)) {
            dialog.setTitle(title);
        }
        dialog.setOnDismissListener(this);
        if (state != null) {
            dialog.onRestoreInstanceState(state);
        }

        // Add the screen to the list of preferences screens opened as dialogs
        getPreferenceManager().addPreferencesScreen(dialog);

        dialog.show();
    }

The way that I work around it is to set the parent background color by overriding onCreateView in the first preference added to the preference screen. Of course this requires some custom code but it's not terribly complicated, for instance to set a white background:

package com.justinbuser.livewallpapers;

import android.preference.PreferenceCategory;

public class VideoChooserPreferenceCategory extends PreferenceCategory{

    public VideoChooserPreferenceCategory(Context context) {
        super(context);
    }

    @Override
    protected View onCreateView(ViewGroup parent)
    {
        parent.setBackgroundColor(0xFFFFFFFF);
        return super.onCreateView(parent);
    }
}

You would then of course need to use that custom category by altering your xml, i.e.:

<PreferenceScreen android:title="main preferences">
    <PreferenceScreen android:title="sub screen">
    <com.justinbuser.livewallpapers.VideoChooserPreferenceCategory android:title="sub screen category" />
    </PreferenceScreen>
</PreferenceScreen>

Also, if you notice the android PreferenceScreen changes the theme based on whether or not a title is set, i.e. if a title exists it enables a theme that includes the title bar. So if you want no title bar you should avoid setting the preferencescreen title and set it statically in xml or dynamically through code.


Have you tried this?

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference){
        super.onPreferenceTreeClick(preferenceScreen, preference);
        if (preference!=null)
            if (preference instanceof PreferenceScreen)
                if (((PreferenceScreen)preference).getDialog()!=null)
                    ((PreferenceScreen)preference).getDialog().getWindow().getDecorView().setBackgroundDrawable(this.getWindow().getDecorView().getBackground().getConstantState().newDrawable());
        return false;
    }

Add this method in your PreferenceActivity.

At comment #35 from this source.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜