I am IT king demon King this is my second IT original
Try to write the best blog for mid-level Android developers
If you don’t add an ID to the root View of your Activity, how can you get the View?
It doesn’t matter if YOU don’t understand the question, let me explain it to you again
The layout file for the Activity is shown below
<? xml version="1.0" encoding="utf-8"? > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvTest"
android:textSize="30sp"
android:textColor="#ffffff"
android:text="I'm the layout in the Activity"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/colorAccent" />
</RelativeLayout>
Copy the code
The code in the Activity looks like this:
public class ViewTreeMainActivity extends AppCompatActivity {
private static final String TAG = "ViewTreeMainActivity===";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_tree_main);
findViewById(R.id.tvTest).setOnClickListener(new View.OnClickListener() {@override public void onClick(View v) {// TODO: RelativeLayout}}; }}Copy the code
Notice the logic we need to implement in TODO, which is the interviewer’s question.
If you understand the question and have an answer, you can “like” it and leave.
If you don’t understand the question, but you have an answer, I believe you.
If you did not understand the question and do not have an answer, please review the question again.
If you understand the question but don’t have an answer, then you should move on
Now I’m going to reveal the answer, just two lines of code
FrameLayout rootView = findViewById(android.R.id.content);
RelativeLayout relativeLayout = (LinearLayout) rootView.getChildAt(0);
Copy the code
If you run into an interviewer who is particularly lukewarm, you can now copy the assignment and leave.
Remember the thumb up
Sadly, the usual response is, why?
Yes, why is id written android.r.I.D.C. tent? Where does FrameLayout come from?
To see why, we need to start with the View tree structure in Android
The above first
What we’re not familiar with is the structure on the red dotted line, and that’s what we’re going to focus on.
A quick explanation:
1. Each Activity has a Window. The Window displays our interface, and the Activity manages the Window. 2, Every Window has a root View– >DecorView. Windows can’t display the interface by themselves, and Android displays the real screen only through the View.
3. DecorView is a FrameLayout to which our Activity layout file was added.
Don’t get it, do you? It’s okay. Keep watching
The structure looks something like this:
An Activity is a Window frame that manages Windows, such as opening, closing, and so on.
A DecorView is like putting a blank sheet of paper on each window to show us the real picture
The setContentView is the View that you add to the DecorView after the Activity layout is synchronized into a View, like adding a View to the white paper in the image above.
Our Activity layout is a window cut.
In other words, what a window looks like is determined by the cut on it.
Because the window itself is transparent
Read this over and over again, because this is as good as I can get. You’ll have to wait two more years for me to make this easier.
But I suggest you take a moment to think. What if I change careers in a year or two?
Let’s move on
In this structure, we need to understand the structure of the DecorView
Same old rule. Picture first
What does this picture mean
So the DecorView itself is a FrameLayout and that FrameLayout has a LinearLayout and we know that the LinearLayout is either horizontal or vertical, in this case it’s vertical.
The vertical LinearLayout is divided into two parts, each part has the function of each part
The upper part is the ActionBar
The bottom part is FrameLayout, and that FrameLayout has an ID, and that ID is the content
In the Activity setContentView we add the Activity layout file to the FrameLayout with the CONTENT id.
Because the ID is content, our API name is setContentView
All right, let me summarize this with a little bit of a three-dimensional picture
The last piece of code you’ve never seen before
public class ViewTreeMainActivity extends AppCompatActivity {
private static final String TAG = "ViewTreeMainActivity==="; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Public View inflate = view.inflate (this, r.layout.activity_view_tree_main,null) FrameLayout content = findViewById(Android.r.i.C.ontent); AddView (rootView); // Add the layout file to FrameLayout }}Copy the code
If you’re serious about learning, you should click on this code and you’ll be surprised to see that our Activity still works.
And in fact, setContentView does exactly that.
Don’t look at the source code, it’s not written in the source code.
If you are interested, you can check out the phoneWindow setContentView source code.
Now let’s go back to the interview questions mentioned at the beginning of the article and the answers I gave
How do I get this View without adding an ID to the root View of the Activity?
FrameLayout rootView = findViewById(android.R.id.content);
RelativeLayout relativeLayout = (LinearLayout) rootView.getChildAt(0);
Copy the code
Now you see why the answer is written that way?
Finally, there are a few questions:
In order to make it easier for everyone to understand the above picture, I used a wrong expression: the layout of the Activity, which is deliberately not strict, in order to prevent someone to offend, hereby declare.
Whether the ActionBar is displayed or not, and the background color of the Window are all related to the Activity theme.
My question is when the ActionBar is not displayed, how does it work? Did he hide it “unavailable” in a “setVisiable” way?
We can dynamically hide an Activity’s ActionBar with the requestWindowFeature(window.feature_no_title). This line of code needs to be called before the setContentView.
My question is, why does it have to be called before setContentView?
In addition, this structure is not detailed enough, in order to reduce the difficulty of understanding and learning (in fact, NO matter how detailed I will not) after you master these contents, it will be easier to accept others’ writing in more detail
Wait, one more question. What’s the use of knowing this?
If you want to know the story, listen to the next episode.