אפשר ליצור היררכיה באופן פרוגרמטי ב-
onCreatePreferences()
הדוגמה הבאה ממחישה גישה פרוגרמטית ליצירת
במסך ההעדפות שנוצר באמצעות XML
סקירה כללית. שפת תרגום
ליצור את המסך באופן פרוגרמטי, ליצור כל הגדרה ולקבוע את הרלוונטיות שלה
ומוסיפים אותו למסך ההעדפות:
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);}
הוספת
PreferenceCategory הוא
דומה. הדוגמה הבאה ממחישה גישה פרוגרמטית ליצירת
מסך ההעדפות שמוצג
לארגן את ההגדרות.
הצאצאים מתווספים אל PreferenceCategory ולא לשורש
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);}
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-27 (שעון UTC).
[null,null,["עדכון אחרון: 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."]]