在代码中创建层次结构 Android Jetpack 的一部分。
您可以在 onCreatePreferences()
中以编程方式创建层次结构。以下示例演示了以编程方式创建与通过 XML 在 概述 页面上创建的相同首选项屏幕的程序化方法。要以编程方式创建屏幕,请创建每个设置并设置其相关属性,然后将其添加到首选项屏幕
Kotlin
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { val context = preferenceManager.context val screen = preferenceManager.createPreferenceScreen(context) val notificationPreference = SwitchPreferenceCompat(context) notificationPreference.key = "notifications" notificationPreference.title = "Enable message notifications" val feedbackPreference = 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
@Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { Context context = getPreferenceManager().getContext(); PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context); SwitchPreferenceCompat notificationPreference = new SwitchPreferenceCompat(context); notificationPreference.setKey("notifications"); notificationPreference.setTitle("Enable message notifications"); Preference feedbackPreference = new Preference(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
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { val context = preferenceManager.context val screen = preferenceManager.createPreferenceScreen(context) val notificationPreference = SwitchPreferenceCompat(context) notificationPreference.key = "notifications" notificationPreference.title = "Enable message notifications" val notificationCategory = PreferenceCategory(context) notificationCategory.key = "notifications_category" notificationCategory.title = "Notifications" screen.addPreference(notificationCategory) notificationCategory.addPreference(notificationPreference) val feedbackPreference = Preference(context) feedbackPreference.key = "feedback" feedbackPreference.title = "Send feedback" feedbackPreference.summary = "Report technical issues or suggest new features" val helpCategory = PreferenceCategory(context) helpCategory.key = "help" helpCategory.title = "Help" screen.addPreference(helpCategory) helpCategory.addPreference(feedbackPreference) preferenceScreen = screen }
Java
@Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { Context context = getPreferenceManager().getContext(); PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context); SwitchPreferenceCompat notificationPreference = new SwitchPreferenceCompat(context); notificationPreference.setKey("notifications"); notificationPreference.setTitle("Enable message notifications"); PreferenceCategory notificationCategory = new PreferenceCategory(context); notificationCategory.setKey("notifications_category"); notificationCategory.setTitle("Notifications"); screen.addPreference(notificationCategory); notificationCategory.addPreference(notificationPreference); Preference feedbackPreference = new Preference(context); feedbackPreference.setKey("feedback"); feedbackPreference.setTitle("Send feedback"); feedbackPreference.setSummary("Report technical issues or suggest new features"); PreferenceCategory helpCategory = new PreferenceCategory(context); helpCategory.setKey("help"); helpCategory.setTitle("Help"); screen.addPreference(helpCategory); helpCategory.addPreference(feedbackPreference); setPreferenceScreen(screen); }