我已将 SettingsActivity 添加到我的项目中。默认情况下,Android Studio 创建了 3 个类别和 3 个标题。当我点击类别时,会出现新设置屏幕。 我不想要那个。我的应用程序的设置选项非常有限,所以我只想获得平面设置列表,没有任何标题和类别。
我发现的,不工作或弃用的 api。你能给我正确、有效的指导,说明如何在不使用任何已弃用的 api 的情况下实现平面设置屏幕吗?
最佳答案
在 SettingActivity
的 onCreate()
方法中添加通用 fragment ,并在 GeneralPreferenceFragment
中进行任何您喜欢的更改
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
this.getFragmentManager().beginTransaction().replace(android.R.id.content, new GeneralPreferenceFragment()).commit();
}
并覆盖 GeneralPreferenceFragment
中的 onOptionItemSelected
,如下所示:
public static class GeneralPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("your_key"));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
getActivity().onBackPressed();
}
return super.onOptionsItemSelected(item);
}
}
关于Android AppCompat PreferenceActivity - 没有子类别/标题的平面设置列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43514231/