Sie können eine Hierarchie programmatisch in
onCreatePreferences()
Das folgende Beispiel zeigt einen programmatischen Ansatz,
Bildschirm mit Einstellungen, der über XML im
Übersicht angezeigt. Bis
den Bildschirm programmatisch erstellen, jede Einstellung erstellen und ihre relevanten
Eigenschaften hinzu und fügen Sie sie dann dem Bildschirm mit den Einstellungen hinzu:
Kotlin
overridefunonCreatePreferences(savedInstanceState:Bundle?,rootKey:String?){valcontext=preferenceManager.contextvalscreen=preferenceManager.createPreferenceScreen(context)valnotificationPreference=SwitchPreferenceCompat(context)notificationPreference.key="notifications"notificationPreference.title="Enable message notifications"valfeedbackPreference=Preference(context)feedbackPreference.key="feedback"feedbackPreference.title="Send feedback"feedbackPreference.summary="Report technical issues or suggest new features"screen.addPreference(notificationPreference)screen.addPreference(feedbackPreference)preferenceScreen=screen}
Java
@OverridepublicvoidonCreatePreferences(BundlesavedInstanceState,StringrootKey){Contextcontext=getPreferenceManager().getContext();PreferenceScreenscreen=getPreferenceManager().createPreferenceScreen(context);SwitchPreferenceCompatnotificationPreference=newSwitchPreferenceCompat(context);notificationPreference.setKey("notifications");notificationPreference.setTitle("Enable message notifications");PreferencefeedbackPreference=newPreference(context);feedbackPreference.setKey("feedback");feedbackPreference.setTitle("Send feedback");feedbackPreference.setSummary("Report technical issues or suggest new features");screen.addPreference(notificationPreference);screen.addPreference(feedbackPreference);setPreferenceScreen(screen);}
Hinzufügen eines
PreferenceCategory ist
ähnlich. Das folgende Beispiel zeigt einen programmatischen Ansatz für
Bildschirm mit den Einstellungen
Einstellungen verwalten
Die untergeordneten Elemente werden dem PreferenceCategory und nicht dem Stamm hinzugefügt
PreferenceScreen.
Kotlin
overridefunonCreatePreferences(savedInstanceState:Bundle?,rootKey:String?){valcontext=preferenceManager.contextvalscreen=preferenceManager.createPreferenceScreen(context)valnotificationPreference=SwitchPreferenceCompat(context)notificationPreference.key="notifications"notificationPreference.title="Enable message notifications"valnotificationCategory=PreferenceCategory(context)notificationCategory.key="notifications_category"notificationCategory.title="Notifications"screen.addPreference(notificationCategory)notificationCategory.addPreference(notificationPreference)valfeedbackPreference=Preference(context)feedbackPreference.key="feedback"feedbackPreference.title="Send feedback"feedbackPreference.summary="Report technical issues or suggest new features"valhelpCategory=PreferenceCategory(context)helpCategory.key="help"helpCategory.title="Help"screen.addPreference(helpCategory)helpCategory.addPreference(feedbackPreference)preferenceScreen=screen}
Java
@OverridepublicvoidonCreatePreferences(BundlesavedInstanceState,StringrootKey){Contextcontext=getPreferenceManager().getContext();PreferenceScreenscreen=getPreferenceManager().createPreferenceScreen(context);SwitchPreferenceCompatnotificationPreference=newSwitchPreferenceCompat(context);notificationPreference.setKey("notifications");notificationPreference.setTitle("Enable message notifications");PreferenceCategorynotificationCategory=newPreferenceCategory(context);notificationCategory.setKey("notifications_category");notificationCategory.setTitle("Notifications");screen.addPreference(notificationCategory);notificationCategory.addPreference(notificationPreference);PreferencefeedbackPreference=newPreference(context);feedbackPreference.setKey("feedback");feedbackPreference.setTitle("Send feedback");feedbackPreference.setSummary("Report technical issues or suggest new features");PreferenceCategoryhelpCategory=newPreferenceCategory(context);helpCategory.setKey("help");helpCategory.setTitle("Help");screen.addPreference(helpCategory);helpCategory.addPreference(feedbackPreference);setPreferenceScreen(screen);}
Alle Inhalte und Codebeispiele auf dieser Seite unterliegen den Lizenzen wie im Abschnitt Inhaltslizenz beschrieben. Java und OpenJDK sind Marken oder eingetragene Marken von Oracle und/oder seinen Tochtergesellschaften.
Zuletzt aktualisiert: 2025-07-27 (UTC).
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Benötigte Informationen nicht gefunden","missingTheInformationINeed","thumb-down"],["Zu umständlich/zu viele Schritte","tooComplicatedTooManySteps","thumb-down"],["Nicht mehr aktuell","outOfDate","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Problem mit Beispielen/Code","samplesCodeIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-07-27 (UTC)."],[],[],null,["# Create a hierarchy in code\nPart of [Android Jetpack](/jetpack).\n===============================================================\n\nYou can create a hierarchy programmatically in\n[`onCreatePreferences()`](/reference/androidx/preference/PreferenceFragmentCompat#oncreatepreferences).\nThe following example demonstrates a programmatic approach to creating the same\npreference screen created through XML on the\n[overview](/develop/ui/views/components/settings#create_a_hierarchy) page. To\ncreate the screen programmatically, create each setting and set its relevant\nproperties, then add it to the preference screen: \n\n### Kotlin\n\n```kotlin\noverride fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {\n val context = preferenceManager.context\n val screen = preferenceManager.createPreferenceScreen(context)\n\n val notificationPreference = SwitchPreferenceCompat(context)\n notificationPreference.key = \"notifications\"\n notificationPreference.title = \"Enable message notifications\"\n\n val feedbackPreference = Preference(context)\n feedbackPreference.key = \"feedback\"\n feedbackPreference.title = \"Send feedback\"\n feedbackPreference.summary = \"Report technical issues or suggest new features\"\n\n screen.addPreference(notificationPreference)\n screen.addPreference(feedbackPreference)\n\n preferenceScreen = screen\n}\n```\n\n### Java\n\n```java\n@Override\npublic void onCreatePreferences(Bundle savedInstanceState, String rootKey) {\n Context context = getPreferenceManager().getContext();\n PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context);\n\n SwitchPreferenceCompat notificationPreference = new SwitchPreferenceCompat(context);\n notificationPreference.setKey(\"notifications\");\n notificationPreference.setTitle(\"Enable message notifications\");\n\n Preference feedbackPreference = new Preference(context);\n feedbackPreference.setKey(\"feedback\");\n feedbackPreference.setTitle(\"Send feedback\");\n feedbackPreference.setSummary(\"Report technical issues or suggest new features\");\n\n screen.addPreference(notificationPreference);\n screen.addPreference(feedbackPreference);\n\n setPreferenceScreen(screen);\n}\n```\n\nAdding a\n[`PreferenceCategory`](/reference/androidx/preference/PreferenceCategory) is\nsimilar. The following example demonstrates a programmatic approach to creating\nthe preference screen seen in\n[Organize your settings](/develop/ui/views/components/settings/organize-your-settings#preference_categories).\nThe children are added to the `PreferenceCategory` and not to the root\n`PreferenceScreen`. \n\n### Kotlin\n\n```kotlin\noverride fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {\n val context = preferenceManager.context\n val screen = preferenceManager.createPreferenceScreen(context)\n\n val notificationPreference = SwitchPreferenceCompat(context)\n notificationPreference.key = \"notifications\"\n notificationPreference.title = \"Enable message notifications\"\n\n val notificationCategory = PreferenceCategory(context)\n notificationCategory.key = \"notifications_category\"\n notificationCategory.title = \"Notifications\"\n screen.addPreference(notificationCategory)\n notificationCategory.addPreference(notificationPreference)\n\n val feedbackPreference = Preference(context)\n feedbackPreference.key = \"feedback\"\n feedbackPreference.title = \"Send feedback\"\n feedbackPreference.summary = \"Report technical issues or suggest new features\"\n\n val helpCategory = PreferenceCategory(context)\n helpCategory.key = \"help\"\n helpCategory.title = \"Help\"\n screen.addPreference(helpCategory)\n helpCategory.addPreference(feedbackPreference)\n\n preferenceScreen = screen\n}\n```\n\n### Java\n\n```java\n@Override\npublic void onCreatePreferences(Bundle savedInstanceState, String rootKey) {\n Context context = getPreferenceManager().getContext();\n PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context);\n\n SwitchPreferenceCompat notificationPreference = new SwitchPreferenceCompat(context);\n notificationPreference.setKey(\"notifications\");\n notificationPreference.setTitle(\"Enable message notifications\");\n\n PreferenceCategory notificationCategory = new PreferenceCategory(context);\n notificationCategory.setKey(\"notifications_category\");\n notificationCategory.setTitle(\"Notifications\");\n screen.addPreference(notificationCategory);\n notificationCategory.addPreference(notificationPreference);\n\n Preference feedbackPreference = new Preference(context);\n feedbackPreference.setKey(\"feedback\");\n feedbackPreference.setTitle(\"Send feedback\");\n feedbackPreference.setSummary(\"Report technical issues or suggest new features\");\n\n PreferenceCategory helpCategory = new PreferenceCategory(context);\n helpCategory.setKey(\"help\");\n helpCategory.setTitle(\"Help\");\n screen.addPreference(helpCategory);\n helpCategory.addPreference(feedbackPreference);\n\n setPreferenceScreen(screen);\n}\n```\n| **Warning:** Add the `PreferenceCategory` to the `PreferenceScreen` before adding children to it. Preferences can't be added to a `PreferenceCategory` that isn't attached to the root screen."]]