SettingsActivity / Fragment显示布局边界之外的选项

时间:2013-12-20 14:26:58

标签: java android android-layout android-settings

我已经在其中创建了一个带有PreferenceFragment的PreferenceActivity。但是所有设置的内容都显示在布局边界之外,我做错了什么?

Settings

SettingsActivity:

public class SettingsActivity extends PreferenceActivity {
    @Override
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    }

    public static class MyPreferenceFragment extends PreferenceFragment
    {
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
    }
}

的preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 <PreferenceCategory android:title="My list of Preferences">
  <CheckBoxPreference android:title="Checkbox Preference"
   android:defaultValue="false" android:summary="This preference can be true or false"
   android:key="checkboxPref" />
  <EditTextPreference android:name="EditText Preference"
   android:summary="This allows you to enter a string"
   android:defaultValue="Nothing" android:title="Edit This Text"
   android:key="editTextPref" />
  <RingtonePreference android:name="Ringtone Preference"
   android:summary="Select a ringtone" android:title="Ringtones"
   android:key="ringtonePref" />
  <PreferenceScreen android:key="SecondPrefScreen"
   android:title="Secondary Level" android:summary="This is a sub PreferenceScreen">
   <intent android:action="android.intent.action.VIEW"
    android:targetPackage="com.as400samplecode" android:targetClass="com.as400samplecode.Preferences2" />
  </PreferenceScreen>
  <Preference android:title="Custom Preference"
   android:summary="This works almost like a button" android:key="customPref" />
 </PreferenceCategory>
</PreferenceScreen>

2 个答案:

答案 0 :(得分:0)

我通过使用普通的Activity而不是PreferenceActivity来解决它。

答案 1 :(得分:-1)

您不是使用SharedPreferences类添加的首选项代码 我添加了一个使用您的复选框首选项示例检查下面的代码

    public class PrefsFragment extends PreferenceFragment {    
    @Override    
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);      
addPreferencesFromResource(R.xml.preferences);     
}    
    }

// xml Preference.xml

<PreferenceCategory
        android:title="PreferenceCategory A">

    <CheckBoxPreference
            android:key="checkbox_preference"
            android:title="title_checkbox_preference"
            android:summary="summary_checkbox_preference" />

</PreferenceCategory>

<PreferenceCategory
        android:title="PreferenceCategory B">

    <EditTextPreference
            android:key="edittext_preference"
            android:title="title_edittext_preference"
            android:summary="summary_edittext_preference"
            android:dialogTitle="dialog_title_edittext_preference" />

</PreferenceCategory>