Android5.1 system Settings main interface UI display process source code analysis

I am a car maker, so the system settings will often need to be modified. At first, I was helpless. Later, I searched for information, and after reading it carefully, I was basically able to cope with the work content. Now I record it, I am afraid I will forget it. Also for your reference, sorry for the bad writing


5.1 The main interface of the system settings application is Settings.java, which inherits from SettingsActivity.java, and SettingsActivity inherits from Activity, so the main interface of settings is essentially an Activity; first, start from the onCreate() method of the parent class SettingsActivity, and come to setContentView (mIsShowingDashboard?R.layout.settings_main_dashboard : R.layout.settings_main_prefs), because mIsShowingDashboard is true, the loaded layout file is R.layout.settings_main_dashboard, which is located in ndroid\packages\apps\Settings\res\layout, code show as below:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/main_content"
             android:layout_height="match_parent"
             android:layout_width="match_parent"
             android:background="@color/dashboard_background_color"
             />
Then we came to the line of code switchToFragment(DashboardSummary.class.getName(), null, false, false, mInitialTitleResId, mInitialTitle, false), why did we come here? It is also because mIsShowingDashboard is true, through this line of code to switch When it comes to the Fragment of DashboardSummary.java, the class of DashboardSummary.java is in the android\packages\apps\Settings\src\com\android\settings\dashboard\ directory. Since DashboardSummary.java is a Fragment, then we directly look at the onCreateView() method, This method loads an R.layout.dashboard layout with the following code:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dashboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbarStyle="outsideOverlay"
    android:clipToPadding="false">
            <LinearLayout
                android:id="@+id/dashboard_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:paddingStart="@dimen/dashboard_padding_start"
android:paddingEnd="@dimen/dashboard_padding_end"
                android:paddingTop="@dimen/dashboard_padding_top"
                android:paddingBottom="@dimen/dashboard_padding_bottom"
                android:orientation="vertical"
                />

</ScrollView>


It can be seen that all the bluetooth-like and wifi entries in the main interface of Settings are in the LinearLayout whose id is @+id/dashboard_container. After walking through the
onCreateView() method, it comes to the onResume() method, in this method The sendRebuildUI() method is called. In this method, a message of MSG_REBUILD_UI is sent through the Handler object. After receiving the message, the Handler calls rebuildUI(context). The rebuildUI() method is to initialize all the text and text that can be seen in the Settings main interface. The display of small icons. Let's look at 
the line of code List<DashboardCategory> categories = ((SettingsActivity) context).getDashboardCategories(true), we can know that the getDashboardCategories(true) method is a method in the SettingsActivity class, otherwise why is it forced? Right? Then we go back to the getDashboardCategories() method in the SettingsActivity class, which calls the buildDashboardCategories(mCategories) method, which calls the loadCategoriesFromResource(R.xml.dashboard_categories, categories) method, which parses an R through Xml The layout file of .xml.dashboard_categories, the xml layout file is located in the android\packages\apps\Settings\res\xml\ directory, you can open and look at this xml file to understand what is in it, there must be a sudden understanding
Lang's feeling! After parsing, a collection object of type List<DashboardCategory> is returned to the DashboardSummary.java class through the getDashboardCategories() method. Note that getDashboardCategories() is the method in the DashboardSummary.java class mentioned above. This method is called to jump to the SettingsActivity class to track the code. Now that the tracking is completed, we must go back to the original call. We then go back to rebuidUI() in the DashboardSummary.java class. The following code is All subviews are displayed through two nested for loops. There are also two classes DashboardCategory and DashboardTile.java in the two for loops. You can take a look at these two classes by yourself. Why are there two nested for loops? Because the getDashboardCategories() method returns a collection of type List<DashboardCategory>, the generic type DashboardCategory class is actually a large module, which can be understood as a container, which also has subviews, for example For example: [Wireless and Network] big module has wifi, bluetooth and other sub-modules, [Device] big module has display, storage, application and other sub-modules, [Personal] big module has location information, security, account, language and input method and other sub-modules, so it is two nested for loops to initialize all sub-views; let's talk about the content of the file R.xml.dashboard_categories.xml now, I sold it before, I didn't pay attention Said, I mainly want to trace the source code first and then talk about the xml layout file; in this dashboard_categories.xml file, there is a dashboard-categories node, which is a general container, and there is a dashboard-category node below it, and dashboard-category is also a container, It corresponds to the DashboardCategory.java class, and there is a dashboard-title node below it,It corresponds to the DashboardTile.java class. Remember that there are two classes in the two nested for loops mentioned above? Yes, it corresponds to those two classes. The dashboard-title node is the display effect of the specific module. For example, WLAN and Bluetooth entry sub-modules, dashboard-title nodes also have attributes such as id, icon, title, fragment, etc., click to jump to the fragment of the corresponding full class name; the click event is implemented in the DashBoardTitleView class, in the construction The set click event listener, in the rebuildUI() method in the DashBoardSummary class, traverses each DashBoardTitleView object through the second for loop in the nested two for loops. At this point, the UI display process of the Settings main interface is analyzed. It's over; having said so much, take a look The contents of the file R.xml.dashboard_categories.xml, just post a little bit, as follows:




------------------------------------------------//-----------------------------------



Summarize

1. Everyone is familiar with the UI display process of the Settings main interface, so you will not have such a headache when you encounter the need for modification at work. Students who do car machines will definitely encounter   this kind of demand. No one takes the system source code with it. It's very painful, but I still can't understand it. I can only understand it slowly after reading other people's materials. Thanks to the great gods who shared it.

2. It is hereby explained that the above code is only the system Settings source code of the Android5.1 platform. The source code and file location of other platforms may be different. Note that

3. In fact, the above is the UI display process of the Settings main interface. It is actually an Activity, and then a Fragment of     DashboardSummary.java is installed, that's all

4. The files mentioned above are in the following order: so that you can track the code yourself

[SettingsActivity.java]  [settings_main_dashboard.xml] [DashboardSummary.java] [dashboard.xml] 

[dashboard_categories.xml] [DashboardCategory.java] [DashboardTile.java] [DashboardTileView.java]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325938066&siteId=291194637