PreferenceFragment不会显示在Activity中

Ole*_*nov 6 settings android preferences

我已经按照android开发人员的指南"设置"进行了操作.但是当我启动我的SettingsActivity时,我得到的只是白屏.我究竟做错了什么?

这是我的SettingActivity.java

public class SettingsActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getFragmentManager().beginTransaction()
                 .replace(android.R.id.content, new SettingsFragment())
                 .commit();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是SettingFragment

public class SettingsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是首选项文件

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference android:title="Your Name"
        android:key="username"
        android:summary="Please provide your username"></EditTextPreference>
    <CheckBoxPreference android:title="Application Updates"
        android:defaultValue="false"
        android:summary="This option if selected will allow the application to check for latest versions."
        android:key="applicationUpdates" />
    <ListPreference     android:title="Download Details"
        android:summary="Select the kind of data that you would like to download"
        android:key="downloadType"
        android:defaultValue="1"
        android:entries="@array/listArray"
        android:entryValues="@array/listValues" />
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

这就是我切换到SettingsActivity的方式

Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                            startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

白屏应该发生吗?我期待一个带设置的默认屏幕

Ole*_*nov 2

我找到了解决方案。

屏幕不是空白的,我的主题使所有文本在白色背景上变成白色。

所以我只是为设置活动添加了一个自定义主题,并将其添加到 Android 清单中:

        android:theme="@style/SettingsTheme"
Run Code Online (Sandbox Code Playgroud)