我无法在横向方向的 10 英寸设备上正确显示首选项。它显示类别标题和复选框真的很奇怪。请参阅附图以了解此想法。
Prefs-XML (仅类别,原因字段稍后在代码中添加):
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory
android:key="settingsCategory"
android:title="@string/mobile_global_settings" >
</PreferenceCategory>
在 SettingsActivity 我添加 PreferenceFragment 像这样:
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Tp_actionbar_style);
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
我的 PreferenceFragment 看起来像这样(我必须为登录到应用程序的不同用户使用不同的 settings_keys):
public static class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
TPSettingsActivity act = (TPSettingsActivity)getActivity();
String userName = "xxx";
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
PreferenceCategory targetCategory = (PreferenceCategory)findPreference("settingsCategory");
CheckBoxPreference checkBoxPreference = new CheckBoxPreference(getActivity());
checkBoxPreference.setKey(userName + "_autoclear");
checkBoxPreference.setChecked(true);
checkBoxPreference.setSummary(R.string.mobile_settings_onoff);
checkBoxPreference.setTitle(R.string.mobile_settings_autoclean);
checkBoxPreference.setChecked(prefs.getBoolean(userName + "_autoclear", false));
targetCategory.addPreference(checkBoxPreference);
TPListPreference listPref = new TPListPreference(getActivity());
listPref.setKey(userName + "_autoclearperiod");
listPref.setEntries(R.array.autoclearOptions);
listPref.setEntryValues(R.array.autoclearValues);
listPref.setSummary(R.string.mobile_settings_currentinterval);
listPref.setTitle(R.string.mobile_settings_interval);
listPref.setValue(prefs.getString(userName + "_autoclearperiod", "7"));
targetCategory.addPreference(listPref);
}
}
当使用非应用特定主题时也会发生这种情况。我使用的主题是使用 Android Asset Studio 站点上列出的 ThemeGenerator 的生成器: Android Asset Studio