Settings series (3) Dynamically add and delete the setting items of the first-level menu and the second-level menu according to the needs

1. Background

At that time, I encountered customization requirements and needed to hide the menu items of Settings according to actual needs, so I started looking for a method

2. Preparation

After looking at the source code, after trying it out and confirming that it takes effect, I will briefly explain the main components of the layout in Settings

The menu items in Settings are composed of PreferenceScreen and Preference. Among them, PreferenceScreen is similar to the layout in the layout we usually use, and Preference is equivalent to the control in the layout. There is a key attribute in PreferenceScreen and Preference similar to the id in the system space we use. In Settings, use the key to locate the Preference where it is located. For example, the following example is the layout in the system menu

packages\apps\Settings\res\xml\system_dashboard_fragment.xml

It should be noted here that there are two more important parts in the Preference besides the existence of the key, that is, the two tags of android:fragment and settings:controller, one indicates the fragment that clicks the menu item to jump, and the other indicates the menu item corresponding control class.

<?xml version="1.0

Guess you like

Origin blog.csdn.net/fighting_2017/article/details/131936404