Android PreferencesActivity显示上一个片段

时间:2016-04-20 11:49:05

标签: android android-preferences

我根据示例here扩展了PreferencesActivity,创建了一个“首选项”屏幕。

第一个屏幕显示没问题但是当我点击查看第二个屏幕时,我看到它们一个在另一个上面。 因此,从其他一些线程中获取建议,我将第二个片段的背景更改为黑色。

这种方式起作用,因为我不再看到它们。但相反,除了标题之外,我只看到第一个。

第一个屏幕如下所示:

enter image description here

,第二个看起来像这样:

enter image description here

只有标题行改变了,而其余的则保持不变。

这是我活动中的代码:

public class SettingsActivity  extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Load the preferences from an XML resource
    getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesMain()).commit();


}

/**
 * This fragment contains a second-level set of preference that you
 * can get to by tapping an item in the first preferences fragment.
 */
public static class PreferencesMain extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public void onStart() {
        super.onStart();
        View view = getView();
        view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
    }
}

/**
 * This fragment contains a second-level set of preference that you
 * can get to by tapping an item in the first preferences fragment.
 */
public static class PreferencesNotifications extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences_notifications);

    }

    @Override
    public void onStart() {
        super.onStart();
        View view = getView();
        view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
    }
}

@Override
protected boolean isValidFragment (String fragmentName) {
    return true;
}

}

的preferences.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:title="@string/settings_notifications">

    <!-- This PreferenceScreen tag sends the user to a new fragment of
         preferences.  If running in a large screen, they can be embedded
         inside of the overall preferences UI. -->
    <PreferenceScreen
        android:fragment="activities.SettingsActivity$PreferencesNotifications"
        android:title="@string/settings_notifications_managePushTitle"
        android:summary="@string/settings_notifications_managePushSummary">
        android:background="@android:color/black"
        <!-- Arbitrary key/value pairs can be included for fragment arguments -->
        <extra android:name="someKey" android:value="somePrefValue" />
    </PreferenceScreen>

</PreferenceCategory>

preferences_notifications.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

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

<CheckBoxPreference
    android:key="checkbox_preference"
    android:title="@string/settings_notifications_managePushEntry1Title"
    android:summary="@string/settings_notifications_managePushEntry1Summary" />
</PreferenceCategory>

1 个答案:

答案 0 :(得分:1)

添加header.xml以管理设置,并使用onBuildHeaders方法。

资源 headers.xml

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
    <header android:title="Digle"
        android:fragment="activities.SettingsActivity$PreferencesMain"/>
</preference-headers>

Class SettingsActivity

public class SettingsActivity extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Remove all code in here
    }

    @Override
    public void onBuildHeaders(List<Header> target) {
        super.onBuildHeaders(target);
        loadHeadersFromResource(R.xml.headers, target);
    }

    ...
}

修改 替代方案可以是subscreens。您必须将Preference个对象组放在PreferenceScreen

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                  android:key="Settings"
                  android:title="@string/app_name">
    <PreferenceCategory
        android:title="@string/settings_notifications">

        <PreferenceScreen
            android:background="@android:color/black"
            android:summary="@string/settings_notifications_managePushSummary"
            android:title="@string/settings_notifications_managePushTitle">

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

                <CheckBoxPreference
                    android:key="checkbox_preference"
                    android:summary="@string/settings_notifications_managePushEntry1Summary"
                    android:title="@string/settings_notifications_managePushEntry1Title"/>

            </PreferenceCategory>

        </PreferenceScreen>

    </PreferenceCategory>

</PreferenceScreen>