简体   繁体   English

首选项片段未显示首选项

[英]Preference Fragment not showing preferences

I'm trying to get a preference screen to load, but it does not seem to be showing. 我正在尝试加载偏好设置屏幕,但似乎没有显示。 I am able to load the Activity, but I can see no actual preferences. 我可以加载“活动”,但看不到实际的首选项。 I have tried other solution on StackOverflow like adding setContentView(R.layout.activity_settings); 我已经尝试过其他关于StackOverflow的解决方案,例如添加setContentView(R.layout.activity_settings); to the SettingsActivity, but had no success. 到SettingsActivity,但没有成功。

SettingsActivity.java SettingsActivity.java

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

public class SettingsActivity extends ActionBarActivity {

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

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();
    }

}

preferences.xml 的preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="@string/pref_start"
        android:key="pref_key_storage_settings">
        <CheckBoxPreference
            android:key="pref_key_auto_exit"
            android:summary="@string/pref_summary_exit"
            android:title="@string/pref_title_exit"
            android:defaultValue="false"/>
    </PreferenceCategory>
</PreferenceScreen>

SettingsFragment.java SettingsFragment.java

import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;

public class SettingsFragment extends PreferenceFragment {

    public SettingsFragment() {
    }

    public void onCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

Since your activity extends ActionBarActivity which in turn extends FragmentActivity it can only use support Fragment s and their descendants. 由于您的活动扩展了ActionBarActivity ,而ActionBarActivity又扩展了FragmentActivity因此它只能使用support Fragment及其后代。 However the PreferenceFragment descends from native Fragment . 但是, PreferenceFragment源自本机Fragment Even if your minimum SDK is higher than 11 these two are not able to interoperate. 即使您的最低SDK高于11,这两个也无法互操作。

You have two options: 您有两种选择:

1) Go native preferences 1)遵循本地偏好

Your activity would best extend PreferenceActivity . 您的活动最好扩展PreferenceActivity You can then continue using your current PreferenceFragment . 然后,您可以继续使用当前的PreferenceFragment The downside is you'll have to handle styling on API 11+ (or 14+) and API 21+ separately (but it can be done). 缺点是您必须分别处理API 11+(或14+)和API 21+上的样式(但这可以完成)。 Full documentation on native preferences 有关本机首选项的完整文档

2) EASY: Go full support 2)简单:全面支持

There is a community made backport of PreferenceFragment (actually there are multiple, but I used this one) which can be found here: https://github.com/kolavar/android-support-v4-preferencefragment 有一个社区对PreferenceFragment进行了反向移植(实际上有多个,但是我使用了这个),可以在这里找到: https : //github.com/kolavar/android-support-v4-preferencefragment

You should be able to compile just after importing the library to your project and changing the import statement so instead of import android.preference.PreferenceFragment; 您应该能够在将库导入到项目中并更改import语句后立即进行编译,而不是import android.preference.PreferenceFragment; you'd have import android.support.v4.preference.PreferenceFragment; 您将import android.support.v4.preference.PreferenceFragment; .

How to decide 如何决定

If you target API below 11 and you need the action bar you have no choice but option 2. This option is also best if you don't need the PreferenceActivity sugar like headers, master-detail view on large screens etc. (again see the docs) 如果您将API定位在11以下,并且您需要操作栏,则只能选择选项2。如果不需要PreferenceActivity糖(例如标题,大屏幕上的主从视图等),则该选项也是最佳选择。(再次参见文档)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM