Packaging the DecorView of Activity (2)

Keywords: Android xml


I have seen the modification of the system code of the company to the root layout decor layout.xml

When I started to develop the system a few days ago, I always wanted to change the source code of the system. As for the reason, I always wanted to install it. After stepping on a few pits modified by others, I still felt that on the basis of not changing the source code, it was good to use the source code for myself as much as possible

The code is as follows:

 1     public void wrapDecor(Activity activity) {
 2 
 3         mWindow = activity.getWindow();
 4         if(mWindow == null){
 5             Log.e(TAG, "Window is null");
 6             return;
 7         }
 8         
 9         View decorView = mWindow.getDecorView();
10         if (decorView == null) {
11             Log.e(TAG, "DecorView is null");
12             return;
13         }
14 
15         ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content);
16         if (contentView == null) {
17             Log.e(TAG, "DecorView is null, have you called wrapDecor after Activity#super.onCreate?");
18             return;
19         }
20 
21         final int childCount = contentView.getChildCount();
22         if (childCount == 0) {
23             // Maybe called before Activity#setContentView
24             mPotentialErrorFlag |= FLAG_POTENTIAL_ERROR_SET_CONTENT;
25         }
26 
27         View[] children = new View[childCount];
28         for (int i = 0; i < childCount; i++) {
29             children[i] = contentView.getChildAt(i);
30         }
31 
32         contentView.removeAllViews();
33 
34         LayoutInflater inflater = LayoutInflater.from(activity);
35 
36         //===================== begin ========================
37 
38         // Custom here decor_layout.xml file
39         View wrapper = inflater.inflate(R.layout.decor_layout, null);
40 
41         ViewGroup rawContentView = (ViewGroup) wrapper.findViewById(R.id.content);
42         if (childCount > 0) {
43             for (View child : children) {
44                 rawContentView.addView(child);
45             }
46         }
47         //change for listActivity, add view first then setContenView
48         activity.setContentView(wrapper);
49 
50         //=====================   end   =======================
51 
52         // Get custom decor_layout Control in
53         mOptionsKey = wrapper.findViewById(R.id.feature_bar_options);
54 
55         // What we get here is ActionBar Controls, Due to the large use of ActionBar, 
56         // Overwrite here ActionBar Yes OptionMenu Control
57         ActionBarView actionBarView = (ActionBarView) decorView.findViewById(
58                 com.android.internal.R.id.action_bar);
59         if (actionBarView != null) {
60             // cover ActionBar Yes OptionMenu Control
61             actionBarView.setOverrideOverflowButton(mOptionsKey);
62         } else {
63             Log.d(TAG, "actionBarView is null");
64             if (mWindow != null) {
65                 Log.d(TAG, "Attempt to invoke setShouldOverrideResources access PhoneWindow");
66                 mWindow.setShouldOverrideResources(true);
67             } else {
68                  Log.d(TAG, "mWindow is empty, pls check it");
69             }
70         }
71     }

The core of this code is just between begin and end. The code is very simple, and the idea of packaging, that is, packaging design pattern, is used

Posted by ntjang on Wed, 18 Dec 2019 08:46:18 -0800